Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 ).
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading