I am using / to test my below regular expression but this expression is allowing < character which is not mentioned in the expression.
['!@#$%*\]\[()-=_+{}:\";?,.\/A-Za-z0-9\s]
I am using https://regex101./ to test my below regular expression but this expression is allowing < character which is not mentioned in the expression.
['!@#$%*\]\[()-=_+{}:\";?,.\/A-Za-z0-9\s]
Share
Improve this question
edited Mar 18, 2018 at 11:33
BoltClock
725k165 gold badges1.4k silver badges1.4k bronze badges
asked Feb 1, 2018 at 18:11
DevDev
2951 gold badge4 silver badges22 bronze badges
6
-
2
-
creates a range, escape it. – ctwheels Commented Feb 1, 2018 at 18:13 - can you please explain.. – Dev Commented Feb 1, 2018 at 18:14
-
1
You either need to put
-
at the start or end of the set or escape it with\
such that you end up with['!@#$%*\]\[()\-=_+{}:\";?,.\/A-Za-z0-9\s]
– ctwheels Commented Feb 1, 2018 at 18:15 - @ctwheels : Thanks you.. – Dev Commented Feb 1, 2018 at 18:17
-
You can also use
[!-%'-\/:;=?@[\]{}\w\s]
which is shorthand for everything you wrote (but uses ranges to cover characters instead of explicitly writing them all). If you don't want to you use that you can use a slightly shorter version['!@#$%*\]\[()=+{}:\";?,.\/\w\s-]
that replacesA-Za-z0-9_
with\w
– ctwheels Commented Feb 1, 2018 at 18:33
1 Answer
Reset to default 8-
denotes a range inside a character class.
The range you're matching in your regex is all the characters that appear between ")" and "=", because:
['!@#$%*\]\[()-=_+{}:\";?,.\/A-Za-z0-9\s]
↑ ↑
And the "<" sign appears between them (see here):
You need to:
- escape it, or
- move it to the end (or beginning) of the class
Change to:
['!@#$%*\]\[()=_+{}:\";?,.\/A-Za-z0-9\s-]
Simpler example:
[1-9]
matches digits from "1" to "9", while:
[19-]
and
[1\-9]
matches "1", "9" and "-".