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

Regex: Find all between parenthesis – including if parenthesis exists within

I’m working on a regex to extract a raw string of JS-like functions, where I’d like to extract the method/function name and the parameters/arguments to the method/function:

.replace("hello", "world").trim().replace("chicken", "fried").upper()

Regex: \.(\w+)\((.*?)\)

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

This works for the above string… but if there are parenthesis inside of the surrounding () then it does not match fully.

I’ve been struggling with positive and negative lookahead/behinds and wonder if this is even possible for something like this?

Here is an example below where it does not work:

.replace(/(test)/g, "").trim().replace("", "").upper()

>Solution :

As Wiktor said, a dedicated parser is always the best option for this kind of processing.

However, if you still want to go with the regex route, you could simply add a positive lookahead that look for either a dot, a semi-colon or the end of the string.

\.(\w+)\((.*?)\)(?=\.|$|;)

You can test more cases here: https://regex101.com/r/QJ1h59/1

This is not perfect, though. If the regex inside the call is using a dot right after the parenthesis, the regex will not match the correct string. There will always be cases like this, you would be better off using a dedicated parser.

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