开发者

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

开发者 https://www.devze.com 2023-01-19 09:05 出处:网络
I\'m frustrated开发者_运维问答 with composing regular expressions for Matching \"ABAB\", \"AABB\", \"ABB\", \"AAB\", \"ABAC\" and \"ABCB\".

I'm frustrated开发者_运维问答 with composing regular expressions for Matching "ABAB", "AABB", "ABB", "AAB", "ABAC" and "ABCB".

Let's take "ABAB" for example, all the following string will be matched:

abab
bcbc
1212
xyxy
9090
0909

Which means the RegEx should match a string whose 1st and 3rd characters are same, and 2nd and 4th are also same, but 1st and 2nd should NOT be the same (3rd and 4th should not be same, of course).

Do I make myself clear?

Thanks.

Peter


ABAB like pattern

(\w)(\w(?<!\1))\1\2
  • (\w) match a word character (digit, letter...) and capture the match into backreference 1
  • (\w...) match a word character (digit, letter...) and capture the match into backreference 2
  • (?<!\1) assert that it is impossible to match the regex matched by capturing group number 1 with the match ending at this position (negative lookbehind)
  • \1 match the same text as most recently matched by capturing group number 1
  • \2 match the same text as most recently matched by 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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号