I'm facing an issue to represent a single space in the [] character set expression. I ended up using something like this
([a-zA-Z0-9]|\x20)+
which i think is not very elegant
Update: I now know what is going wrong with my regex. I used something like this which did not work
[A-Za-z0-9/.,':!?$%()- ]+
But when I move the space to be after 9 like this
[A-Za-z0-9 /.,':!?$%()-]+
And it works, just weird. I'm trying this with an online regex tester RegExr.
Update2: I figured out now the issue is that -
inside regex is used to represent a range in []
so anything before and after it has different meaning than just being a plain character
\s stands for “whitespace character”. Again, which characters this actually includes, depends on the regex flavor. In all flavors discussed in this tutorial, it includes [ \t\r\n\f]. That is: \s matches a space, a tab, a carriage return, a line feed, or a form feed.
When rendered, a whitespace character does not correspond to a visible mark, but typically does occupy an area on a page. For example, the common whitespace symbol U+0020 SPACE (also ASCII 32) represents a blank space punctuation character in text, used as a word divider in Western scripts.
Square brackets ( “[ ]” ): Any expression within square brackets [ ] is a character set; if any one of the characters matches the search string, the regex will pass the test return true.
\\. matches the literal character . . the first backslash is interpreted as an escape character by the Emacs string reader, which combined with the second backslash, inserts a literal backslash character into the string being read. the regular expression engine receives the string \.
You can just use a space.
For the regex structure in the question:
([a-zA-Z0-9]| )+
In practice:
[a-zA-Z0-9 ]+
[a-zA-Z0-9 ]
will add a space to the character class. I'm guessing you already know that and you want to make sure that there will be only single spaces in your string. In that case, you need an additional lookahead:
^(?!.* )[a-zA-Z0-9 ]*$
matches a string that contains alphanumerics and spaces, but only single spaces.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With