0

I am trying to set-up a quite complex regexp, but I can't avoid just one element from not-match list.

My regular expression is:

1234567-8_abc((?!_ABC|_DEFGHI)[\w]?)*(\.ios|\.and)

What I have to exclude is:

  • 1234567-8_abc.ios
  • 1234567-8_abc_DEFGHI.ios
  • 1234567-8_abc_ABC.ios

Instead, what I have to include is:

  • 1234567-8_abc_1UP.ios
  • 1234567-8_abc_FI.ios
  • 1234567-8_abc_gmg.ios
  • 1234567-8_abc_1UP.and
  • 1234567-8_abc_FI.and
  • 1234567-8_abc_gmg.and
  • 1234567-8_abc_ddd.and
  • 1234567-8_abc_qwert.ios
  • 1234567-8_abc_88.ios

Well, I can't exclude the first option (1234567-8_abc.ios).

I tried it here.

How can I achieve this?

Thank you!

4 Answers 4

1

You can use this pattern:

1234567-8_abc_[^_.]++(?<!_ABC|_DEFGHI)\.(?:ios|and)

Note: I assume that each substring between _ and .ios doesn't contain a dot or an underscore.

The possessive quantifier ++ is necessary to fail faster with the less possible backtracking steps

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

Comments

0

This regex matches your examples in PHP:

1234567-8_abc_((?!ABC|DEFGHI)[\w]?)*(\.ios|\.and)

Comments

0

Add a negative lookahead like below,

1234567-8_abc(?!_ABC|_DEFGHI)\w+(\.ios|\.and)

DEMO

(?!_ABC|_DEFGHI) Negative lookahead asserts that the string following _abc wouldn't be _ABC or _DEFGHI . And it must have one or more word characters before .ios or .and. So it won't match this 1234567-8_abc.ios string.

Comments

0
1234567-8_abc(?:(?!_ABC|_DEFGHI)\w)+(\.ios|\.and)

Try this.Your regex has left \w after 1234567-8_abc optional.Just made it compulsary.See demo.

http://regex101.com/r/bB8jY7/1

1 Comment

Just a minor note: [\w] can be just \w.

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.