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
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:
matches a string that contains alphanumerics and spaces, but only single spaces.