Reputation: 4903
I am looking for a single regular expression that achieves the following:
/^[a-z0-9-_]+$/i
)/^(?!test).*$/i
)Both requirements are easy to implement on their own. But How would a regular expression look like that combines both requirements in a single expression?
Upvotes: 1
Views: 31
Reputation: 522762
You may just inline your matching logic into the second regex, i.e. use:
^(?!test$)[a-z0-9_-]+$
Note carefully the negative lookahead (?!test$)
. When placed at the very start of the pattern, this asserts that the match is not the exact word test
. However, 1test
and testing
should be allowed, as they are not exactly test
.
Also note that the character class I have used is slightly different than your version. We should place -
at the very end of the class, because 9-_
will actually be interpreted as all characters in between 9
and underscore, which is probably not what you want.
Upvotes: 2