Home > Enterprise >  RegExp avoid double space and space before characters
RegExp avoid double space and space before characters

Time:01-06

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

CodePudding user response:

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

  •  Tags:  
  • Related