user2723285
user2723285

Reputation:

RegEX: Find valid/invalid strings based on condition

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.

  1. If string starts with F or 3 or DC or 9, then it's considered invalid.

  2. 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

Answers (1)

Avinash Raj
Avinash Raj

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-]+$

DEMO

Upvotes: 4

Related Questions