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

Conditional(?) regex for numbers

i was trying to create a regex that could match numbers within brackets or not, for example:

(1.000.000,00) //match
(1.000,00)     //match
(100,00)       //match
(10)           //match
(1)            //match

(2.000.000,00  //dont't match
(2.000,00      //dont't match
(200,00        //dont't match
(20            //dont't match
(2             //dont't match

3.000.000,00)  //dont't match
3.000,00)      //dont't match
300,00)        //dont't match
30)            //dont't match
3)             //dont't match

4.000.000,00   //should match
4.000,00       //should match
400,00         //should match
40             //should match
4              //should match

I need to match only numbers(in brackets or not), but only if they have all brackets (2) or none(0)

At the moment this is what i came up with: \((\d+[\.,]?)+\d*\), it matches the –match and doesn’t match the –don’t match but should match also the –should match

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

I’ve added the javascript tag because i’m using this regex in js and not all of the regex tokens work in the js regex constructor

I’m posting also a regex101 link

>Solution :

If supported, you can usea negative lookbehind to match either with or without parenthesis:

\(\d+(?:[.,]\d+)*\)|(?<!\S)\d+(?:[.,]\d+)*(?!\S)
  • \( Match (
  • \d+(?:[.,]\d+)* Match 1+ digits and optionally repeat matching . or , and again 1+ digits
  • \) Match )
  • | Or
  • (?<!\S) Negative lookbehind, assert a word boundary to the left
  • \d+(?:[.,]\d+)* Match 1+ digits and optionally repeat matching . or , and again 1+ digits
  • (?!\S) Negative lookahead, assert a whitespace boundary to the right

Regex demo

Another option could be matching optional parenthesis at both sides, and only keep the ones that have either an opening and closing parenthesis, or none.

const regex = /\(?\d+(?:[.,]?\d+)*\)?/
const strings = ["(1.000.000,00)", "(1.000,00)", "(100,00)", "(10)", "(1)", "(2.000.000,00", "(2.000,00", "(200,00", "(20", "(2", "3.000.000,00)", "3.000,00)", "300,00)", "30)", "3)", "4.000.000,00", "4.000,00", "400,00", "40", "4"];

strings.forEach(s => {

  const m = s.match(regex);
  const firstChar = s.charAt(0);
  const lastChar = s.charAt(s.length - 1);

  if (
    m &&
    (firstChar !== '(' && lastChar !== ')') ||
    firstChar === '(' && lastChar === ')'
  ) {
    console.log(s)
  }
});
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