I’m new to Regular Expressions…
I’ve been asked a regular expression that accepts Alphanumerics, a few characters more, and only ONE whitespace between words.
For example :
This should match :
"Hello world"
This shouldn’t :
"Hello world"
Any ideas?
This was my expression:
[\w':''.'')''(''\[''\]''{''}''-''_']+$
I already tried the \s? (the space character once or never – right? ) but I didn’t get it to work.
Using Oniguruma regex syntax, you could do something like:
Assuming that the ‘other characters’ are . : () [] {} – _
This regex will match a string that must begin and end with a word character or one of the other allowed characters and cannot have more than one space in a row.
If you’re using the
xflag (ignore whitespace in regular expression) you’ll need to do this instead:The only difference is the
\in front of the space.