Tried
[a-zA-Z0-9-_]
[a-zA-Z0-0\-\_]
[[[:alnum:]]-_]
...
What is the correct way to represent that in regular expression?
It seems like [a-zA-Z0-9-] works for alphanumeric or dash. But when I add another underscore, it breaks.
That will be this character class:
[[:alnum:]_-]
Which means allow one of these:
1. Alpha numeric
1. Underscore
1. Hyphen
It is important to keep hyphen at 1st or last position in character class to avoid escaping.
All of these variations will work:
[a-zA-Z0-9_\-]
[a-zA-Z0-9_-]
[-_a-zA-Z0-9]
[-a-z_A-Z0-9]
[-a-zA-Z_0-9]
[-a-zA-Z0-9_]
...
The hyphen needs to be on one end. It can be escaped or not if at the end, and must be un-escaped if at the beginning. The underscore can be almost anywhere, as long as it's not in the middle of one of your ranges, and doesn't need to be escaped.
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