Matching a^n b^n c^n (for example “aaabbbccc”) using regular expressions in C#

后端 未结 1 1944
感情败类
感情败类 2021-02-05 17:34

You can easily use regex to verify a regular language. My question is can you use it to verify a context-sensitive language? How powerful is the modern regex in the hierarchy?

相关标签:
1条回答
  • 2021-02-05 18:39

    .NET provides balancing groups that you should be able to use to do this; something like:

    ^(?<n>(?<o>a))*(?<-n>b)*(?<-o>c)*(?(n)(?!))(?(o)(?!))$
    

    Increment n and o for each a, decrement n for each b and then o for each c, then fail the match ((?!)) if either counter is still greater than zero.

    0 讨论(0)
提交回复
热议问题