Deepa Aranha
Deepa Aranha

Reputation: 31

Regex - Match a string pattern

I need a regex that will match only strings of this kind

Match these strings:

edo-apple-iphone-5s-i-gold-16

DON'T match these strings:

edo-apple-iphone-5s-i-gold-16-edo-staff-connect-24

The main difference between the 2 strings is either a connect-24 OR handset-24 added to the end of the string

I have written a Regex, but it seems to match both strings:

^edo[a-z1-9\-]*

How do i modify this to not accept if connect-24 OR handset-24 is in the string?

Upvotes: 0

Views: 261

Answers (2)

Andie2302
Andie2302

Reputation: 4897

The regex that matches the requirements: Not accept if connect-24 OR handset-24 is in the string

^(?:(?!connect-24|handset-24).)*$

DEMO

Upvotes: 1

AJMansfield
AJMansfield

Reputation: 4175

Here is a regular expression that would satisfy the requirement as stated:

.{32,}

Clearly, the strings you want to match are all longer than the ones you don't want to match. This regex matches only those strings that are 32 characters or longer, while edo-apple-iphone-5c-i-yellow-32, the longest non-matching string, has only 31 characters.

Upvotes: 0

Related Questions