I have a string -
xyz":abc,"lmn
I want to extract abc. what will be the regular expression for this ?
I am trying this -
/xyz\":(.*?),\"lmn/
But it is not fetching any result.
I have a string -
xyz":abc,"lmn
I want to extract abc. what will be the regular expression for this ?
I am trying this -
/xyz\":(.*?),\"lmn/
But it is not fetching any result.
In c# you could use
var regex = new Regex(@"(?<=xyz\"":).*?(?=,\""lmn)");
var value = regex.Match(@"xyz"":abc,""lmn").Value;
Note this makes use of the c# verbatim string prefix @
that means that \
is not treated as an escape character. You will need to use a double "
so that a single "
will be included in the string.
This regex makes use of prefix and suffix matching rules so that you can get the match without having to select the specific group from the result.
Alternatively you can use group matching
var regex=new Regex(@"xyz\"":(.*?),\""lmn");
var value = regex.Match(@"xyz"":abc,""lmn").Groups[1].Value;
You can check for the existence of a match by doing the following
var match = regex.Match(@"xyz"":abc,""lmn");
var isMatch = match.Success;
and then follow up with either match.Value
or match.Groups[1].Value
depending on which regex you used.
EDIT
Actually the escaping the "
is not needed in a c# regex so you could use either of the following instead.
var regex = new Regex("(?<=xyz\":).*?(?=,\"lmn)");
var regex = new Regex("xyz\":(.*?),\"lmn");
These two do not use the verbatim string prefix, so the \"
is translated into just "
in the regex giving an a regex of (?<=xyz":).*?(?=,"lmn)
or xyz":(.*?),"lmn
Additionally if the is an entire string match rather than a substring you would want one of the following.
var regex = new Regex("(?<=^xyz\":).*?(?=,\"lmn$)");
var regex = new Regex("^xyz\":(.*?),\"lmn$");