Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

488
Vistas
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

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

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

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

about 4 years ago · Juan Pablo Isaza Denunciar

0

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.

about 4 years ago · Juan Pablo Isaza Denunciar

0

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.,)]))).

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda