Ullas Prabhu
Ullas Prabhu

Reputation: 239

Regex match for multiple characters

I want to write a regex pattern to match a string starting with "Z" and not containing the next 2 characters as "IU" followed by any other characters.

I am using this pattern but it is not working Z[^(IU)]+.*$

  1. ZISADR - should match
  2. ZIUSADR - should not match
  3. ZDDDDR - should match

Upvotes: 7

Views: 101782

Answers (2)

Gurmanjot Singh
Gurmanjot Singh

Reputation: 10360

Try this regex:

^Z(?:I[^U]|[^I]).*$

Click for Demo

Explanation:

  • ^ - asserts the start of the line
  • Z - matches Z
  • I[^U] - matches I followed by any character that is not a U
  • | - OR
  • [^I] - matches any character that is not a I
  • .* - matches 0+ occurrences of any character that is not a new line
  • $ - asserts the end of the line

Upvotes: 16

Pushpesh Kumar Rajwanshi
Pushpesh Kumar Rajwanshi

Reputation: 18357

When you want to negate certain characters in a string, you can use character class but when you want to negate more than one character in a particular sequence, you need to use negative look ahead and write your regex like this,

^Z(?!IU).*$

Demo

Also note, your first word ZISADR will match as Z is not followed by IU

Your regex, Z[^(IU)]+.*$ will match the starting with Z and [^(IU)]+ character class will match any character other than ( I U and ) one or more times further followed by .* means it will match any characters zero or more times which is not the behavior you wanted.

Edit: To provide a solution without look ahead

A non-lookahead based solution would be to use this regex,

^Z(?:I[^U]|[^I]U|[^I][^U]).*$

This regex has three main alternations which incorporate all cases needed to cover.

  • I[^U] - Ensures if second character is I then third shouldn't be U
  • [^I]U - Ensures if third character is U then second shouldn't be I
  • [^I][^U] - Ensures that both second and third characters shouldn't be I and U altogether.

Demo non-look ahead based solution

Upvotes: 5

Related Questions