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 modify a regular expression to search between a special string and a character?

I’m trying to write a regular expression to search between a special string and a character.

I have a regular expression:

b(\(.*?)\)

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

It looks for matches between "b(" and ")", for example:
String: az[b('0x611', '0*L3')]
Match: b('0x611', '0*L3')

But sometimes there are lines like this:
String: (G, f[b('0x4ed', 'S)Oi')])

And in this case match will be: b('0x4ed', 'S), and that wrong.

I’m trying to solve this by looking for matches between "b(‘0x4ed’, ‘S)" and ")", but when I try to bring the regular expression to the following form b('0x4ed', 'S)(\(.*?)\), it stops working.

How could I get out of this situation?

>Solution :

Use: "not one or more singlequotes until a singlequote" like: '[^']+'

Example

b\('[^']+', ?'[^']+'\)

Demo on Regex101.com

PS: if the value can be empty '' use zero-or-more *:

b\('[^']+', ?'[^']*'\)
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