0

I know this seems a common question, but can't get rid of my problem dispite searching. I need a regex that matchs only string that doesn't start with a specified set of words and surrounded by /. Example:

/harry/white
/sebastian/red
/tom/black
/tomas/green

I don't want strings starting with /harry/ and /tom/, so I expect

/harry/white     NO
/sebastian/red   YES
/tom/black       NO
/tomas/green     YES

1) ^/(?!(harry|tom)).*    doesn't match /tomas/green
2) ^/(?!(harry|tom))/.*   matchs nothing
3) ^/((harry|tom))/.*     matchs the opposite

What is the right regex? I'd appreciate much if someone explain me why 1 and 2 are wrong. Please don't blame me :) Thanks.

2
  • Can the string end in / like /sebastian/red/ Commented Nov 13, 2013 at 17:54
  • @abc123 yes, it could. Commented Nov 13, 2013 at 19:38

2 Answers 2

2

You need to add the ending slash for both of them inside the negative look-ahead, not outside:

^/(?!(harry|tom)/).*

Not adding a slash, will match tom in tomas, and the negative look-ahead will not satisfy.

Sign up to request clarification or add additional context in comments.

Comments

1

Try:

^(?!/(harry|tom)/).*

Why number 1 is wrong: the lookahead should make sure that harry or tom are followed by a slash.

Why number 2 is wrong: ignore the lookahead; note that the pattern is trying to match two slashes at the start of the string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.