Funny regular expression experience

I was working on a regex to check if all the symbols in the string are from the range of allowed ones.

My initial regex, which  was working was:

^[a-zA-Z0-9_-]*$

Later I decided to allow spaces and dots and wrote a new regex:

^[a-zA-Z0-9_-\. ]*$

This one was not working! I’ve spent quite a time until I figured out that the peace “_-\.” was treated the same way as “a-z”. It assumed I am providing a scope. My issue was fixed after I escaped “-” and my final regex looked like this:

^[a-zA-Z0-9_\-\. ]*$

One thought on “Funny regular expression experience

Leave a Reply