Is there a regular expression to detect a valid regular expression?

前端 未结 8 1121
天命终不由人
天命终不由人 2020-11-22 09:48

Is it possible to detect a valid regular expression with another regular expression? If so please give example code below.

8条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 10:08

    No, if you use standard regular expressions.

    The reason is that you cannot satisfy the pumping lemma for regular languages. The pumping lemma states that a string belonging to language "L" is regular if there exists a number "N" such that, after dividing the string into three substrings x, y, z, such that |x|>=1 && |xy|<=N, you can repeat y as many times as you want and the entire string will still belong to L.

    A consequence of the pumping lemma is that you cannot have regular strings in the form a^Nb^Mc^N, that is, two substrings having the same length separated by another string. In any way you split such strings in x, y and z, you cannot "pump" y without obtaining a string with a different number of "a" and "c", thus leaving the original language. That's the case, for example, with parentheses in regular expressions.

提交回复
热议问题