2

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

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

3 Answers 3

2

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)
  }
});

Sign up to request clarification or add additional context in comments.

2 Comments

Your regex works, can i ask for a summary explanation of this? if you don't want no worries, i'll watch it on regex101. But anyways, that was exactly what it was supposed to match. Thank you so much!
@Nick But of course, I have added a breakdown of the separate parts.
1

If you don't want to repeat the part matching numbers (which in this case is short, so maybe an exception to the DRY rule is warranted), you can reach for \(?((\d+[\.,]?)+\d*)\)?(?<=\(\1\)|(^|[^(\d.,])\1(?=($|[^\d.,)]))).

Comments

1

Edit: this is broken. It will match numbers like (7 as it only matches the number and ignores the parentheses in that case.

Kept here for future reference.

It's usually easier to do regex in multiple passes, but here goes:

/(\((\d+[\.,]?)+\d*\))|(\d+[\.,]?\d*)/gm

You can test it on https://regex101.com/. Usually it's better to process something in multiple passes as you can see the regex becomes even more unreadable. I took your regex and just split it into two regexes: one that requires the parentheses and one that doesn't, then combined them with the or operator. Note that this regex will allow things like "123.3,5.7" as one number, and the capturing groups will be nasty.

3 Comments

I'm upvoting you're answer because of the explanation and the time you spent, but you0re regex matches even if there's just one bracket(that i would like to avoid). Lastly, i know it's not perfect but i know i receive data in a certain format so things like 123.3,5.7 wouldn't be possible
That's unexpected haha.
I see, you want to ensure that in the other case there is no preceding or post regex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.