How to extract text that lies between parentheses

I have string like (CAT,A)(DOG,C)(MOUSE,D)

i want to get the DOG value C using Regular expression.

i tried following

Match match = Regex.Match(rspData, @"\(DOG,*?\)");
if (match.Success)
 Console.WriteLine(match.Value);

But not working could any one help me to solve this issue.

>Solution :

You can use

(?<=\(DOG,)\w+(?=\))?
(?<=\(DOG,)[^()]*(?=\))

See the regex demo.

Details:

  • (?<=\(DOG,) – a positive lookbehind that matches a location that is immediately preceded with (DOG, string
  • \w+ – one or more letters, digits, connector punctuation
  • [^()]* – zero or more chars other than ( and )
  • (?=\)) – a positive lookahead that matches a location that is immediately followed with ).

Leave a Reply