regex - C# Regular Expressions for Matching "ABAB", "AABB", "ABB", "AAB", "ABAC" and "ABCB" -


i'm frustrated composing regular expressions matching "abab", "aabb", "abb", "aab", "abac" , "abcb".

let's take "abab" example, following string matched:

abab bcbc 1212 xyxy 9090 0909 

which means regex should match string 1st , 3rd characters same, , 2nd , 4th same, 1st , 2nd should not same (3rd , 4th should not same, of course).

do make myself clear?

thanks.

peter

abab pattern

(\w)(\w(?<!\1))\1\2 
  • (\w) match word character (digit, letter...) , capture match backreference 1
  • (\w...) match word character (digit, letter...) , capture match backreference 2
  • (?<!\1) assert impossible match regex matched capturing group number 1 match ending @ position (negative lookbehind)
  • \1 match same text matched capturing group number 1
  • \2 match same text matched capturing group number 2

others patterns

  • aabb ==> (\w)\1(\w(?<!\1))\2
  • abb ==> (\w)(\w(?<!\1))\2
  • aab ==> (\w)\1(\w(?<!\1))
  • abac ==> (\w)(\w(?<!\1))\1(\w(?<!\1|\2))
  • abcb ==> (\w)(\w(?<!\1))(\w(?<!\1|\2))\2

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -