Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex match for multiple characters

Tags:

regex

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
like image 765
Ullas Prabhu Avatar asked Dec 06 '22 10:12

Ullas Prabhu


1 Answers

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
like image 113
Gurmanjot Singh Avatar answered Dec 07 '22 22:12

Gurmanjot Singh