c# - Regular Expression For JSON -
i have string -
xyz":abc,"lmn i want extract abc. regular expression ?
i trying -
/xyz\":(.*?),\"lmn/ but not fetching result.
in c# use
var regex = new regex(@"(?<=xyz\"":).*?(?=,\""lmn)"); var value = regex.match(@"xyz"":abc,""lmn").value; note makes use of c# verbatim string prefix @ means \ not treated escape character. need use double " single " included in string.
this regex makes use of prefix , suffix matching rules can match without having select specific group result.
alternatively can use group matching
var regex=new regex(@"xyz\"":(.*?),\""lmn"); var value = regex.match(@"xyz"":abc,""lmn").groups[1].value; you can check existence of match doing following
var match = regex.match(@"xyz"":abc,""lmn"); var ismatch = match.success; and follow either match.value or match.groups[1].value depending on regex used.
edit escaping " not needed in c# regex use either of following instead.
var regex = new regex("(?<=xyz\":).*?(?=,\"lmn)"); var regex = new regex("xyz\":(.*?),\"lmn"); these 2 not use verbatim string prefix, \" translated " in regex giving regex of (?<=xyz":).*?(?=,"lmn) or xyz":(.*?),"lmn
additionally if entire string match rather substring want 1 of following.
var regex = new regex("(?<=^xyz\":).*?(?=,\"lmn$)"); var regex = new regex("^xyz\":(.*?),\"lmn$");
Comments
Post a Comment