Reputation:
Trying to come up with one RegEx pattern that will find valid/invalid strings from a bunch of strings that i get in my program.
The validity of the string is found based on 2 conditions.
If string starts with F or 3 or DC or 9, then it's considered invalid.
If string has anything other than alphanumeric or "-", then its considered invalid.
I want to combine this 2 conditions and come up with one RegEx.
Example strings:
WES897-JK002 // valid string
FDD2+E32FFCC // invalid string
2WWKDFKK0091 // valid string
DCFFF45JJSSD // invalid string
SDSD/8890012 // invalid string
The challenge am facing is to come up with one RegEx pattern combining both the above conditions.
So far i did this, but this doesn't look any good!
^(F|3|DC|9)[A-Z0-9-]+$
Upvotes: 3
Views: 511
Reputation: 174716
You just need to put the first pattern inside a negative lookahead assertion.
^(?!F|3|DC|9)[A-Z0-9-]+$
OR
simply like
^(?![F39]|DC)[A-Z0-9-]+$
Upvotes: 4