Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

151
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda