0

I have read several tutorials on regex and have tried online regex tester, but can't seem to figure out the appropriate regex for the matches I'd like it to fire on.
I'd appreciate help in creating a regex that results in true when

  • the given string contains "AI [num]" where [num] is an integer. There can be anything in front or after this, even "DAI 5b" should match. However, the given string should not return true when it contains "AI 2", again with anything in front or after this allowed.
  • if none of these situations are the case, it should return false

This would mean that

  • "blabla blabla" => false
  • "blabla AI 6" => true
  • "blabla AI 4b" => true
  • "blabla AI 2" => false
  • "blabla AI 2b" => false
  • "blabla AI 6 blabla AI 2b" => false

I really hope someone could point me in the right direction!

1 Answer 1

1

You can use this regex:

AI (?!2)\d+(?!.*AI)

RegEx Demo

RegEx Breakup:

  • AI: Match AI followed by a space
  • (?!2): Using negative lookahead assert that we don't have 2 at next position
  • \d+: Match one or more digits
  • (?!.*AI): Using a negative lookahead make sure we don't have another AI ahead

With MULTILINE modifier, you may use this regex as well:

^(?!.*AI 2).*AI \d+

RegEx Demo 2

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

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.