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

141
Vistas
RegExp avoid double space and space before characters

I'm trying to write a regular expression in order to not allow double spaces anywhere in a string, and also force a single space before a MO or GO mandatory, with no space allowed at the beginning and at the end of the string.

Example 1 : It is 40 GO right

Example 2 : It is 40GO wrong

Example 3 : It is 40 GO wrong

Here's what I've done so far ^[^ ][a-zA-Z0-9 ,()]*[^;'][^ ]$, which prevents spaces at the beginning and at the end, and also the ";" character. This one works like a charm.

My issue is not allowing double spaces anywhere in the string, and also forcing spaces right before MO or GO characters.

After a few hours of research, I've tried these (starting from the previous RegExp I wrote):

To prevent the double spaces: ^[^ ][a-zA-Z0-9 ,()]*((?!.* {2}).+)[^;'][^ ]$

To force a single space before MO: ^[^ ][a-zA-Z0-9 ,()]*(?=\sMO)*[^;'][^ ]$

But neither of the last two actually work. I'd be thankful to anyone that helps me figure this out

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

0

The lookahead (?!.* {2} can be omitted, and instead start the match with a non whitespace character and end the match with a non whitespace character and use a single space in an optionally repeated group.

If the string can not contain a ' or ; then using [^;'][^ ]$ means that the second last character should not be any of those characters.

But you can omit that part, as the character class [a-zA-Z0-9,()] does not match ; and '

Note that using a character class like [^ ] and [^;'] actually expect a single character, making the pattern that you tried having a minimum length.

Instead, you can rule out the presence of GO or MO preceded by a non whitespace character.

^(?!.*\S[MG]O\b)[a-zA-Z0-9,()]+(?: [a-zA-Z0-9,()]+)*$

The pattern matches:

  • ^ Start of string
  • (?!.*\S[MG]O\b) Negative lookahead, assert not a non whitspace character followed by either MO or GO to the right. The word boundary \b prevents a partial word match
  • [a-zA-Z0-9,()]+ Start the match with 1+ occurrences of any of the listed characters (Note that there is no space in it)
  • (?: [a-zA-Z0-9,()]+)* Optionally repeat the same character class with a leading space
  • $ End of string

Regex demo

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