I am currently working on creating a regular expression to ensure there are no double spaces in a string, while also requiring a single space before the characters MO
or GO
, with no spaces allowed at the start or end of the string.
Example 1: It is 40 GO
is correct
Example 2: It is 40GO
is incorrect
Example 3: It is 40 GO
is incorrect
So far, I have this regular expression: ^[^ ][a-zA-Z0-9 ,()]*[^;'][^ ]$
, which successfully prevents spaces at the beginning and end of the string, as well as the ";" character. It's working well.
However, my challenge lies in disallowing double spaces anywhere in the string, and ensuring a space before instances of MO
or GO
.
After extensive research, I have tried these modifications:
To prevent double spaces:
^[^ ][a-zA-Z0-9 ,()]*((?!.* {2}).+)[^;'][^ ]$
To require a space before MO
:
^[^ ][a-zA-Z0-9 ,()]*(?=\sMO)*[^;'][^ ]$
Unfortunately, neither of these changes seem to be working correctly. Any assistance in solving this issue would be greatly appreciated.