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 expression to get numbers without parentheses ()

I’m trying to create a regex that will select the numbers/numbers with commas(if easier, can trim commas later) that do not have a parentheses after and not the numbers inside the parentheses should not be selected either.

Used with the JavaScript’s String.match method

Example strings

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

9(296,178),5,3(123),10
10,9(296,178),2,5,3(123),3(124,125)
10,7,5(296,293,444,1255),3(218),2,4

What i have so far:

/((^\d+[^\(])|(,\d+,)|(,*\d+$))/gm

I tried this in regex101 and underlined the numbers i would like to match and x on the one that should not.

I tried this in regex101 and underlined the numbers i would like to match and x on the one that should not

>Solution :

You could start with a substitution to remove all the unwanted parts:

/\d*\(.*?\),?//gm

Demo

This leaves you with

5,10
10,2,5,
10,7,2,4

which makes the matching pretty straight forward:

/(\d+)/gm

If you want it as a single match expression you could use a negative lookbehind:

/(?<!\([\d,]*)(\d+)(?:,|$)/gm

Demo

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