Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a regular expression itself be parsed with a regular expression? [duplicate]

I am reading the code of a regular expression parser, and start to wonder if the syntax of regular expression is itself regular, and can be expressed with another (quite complicated) regular expression?

rere = "" # the regular expression of regular language
match1 = re.match(rere, "[a-z]+@[a-z]+.com") # True
match2 = re.match(rere, ")az[") # False 

I don't see any recursive structure in regular expression syntax, so I think maybe this is doable?

If it is, what does the expression look like? If not, why?

like image 231
NeoWang Avatar asked Sep 03 '15 06:09

NeoWang


People also ask

How do you repeat a regular expression?

An expression followed by '*' can be repeated any number of times, including zero. An expression followed by '+' can be repeated any number of times, but at least once. An expression followed by '? ' may be repeated zero or one times only.

Can a DFA have multiple regular expressions?

We are interested in What is Regular Expression that is corresponding to DFA . Certainly, there could be multiple Regular Expressions corresponding to it; we prefer a simpler one.

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

No, if you use standard regular expressions. The reason is that you cannot satisfy the pumping lemma for regular languages.

What is correct about regular expression?

A regular expression (shortened as regex or regexp; sometimes referred to as rational expression) is a sequence of characters that specifies a search pattern in text. Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation.


1 Answers

You cannot parse nested parentheses with a regular expression because you would need infinite state to do so. So the answer is no. What you are looking for is called context-free grammars.

like image 147
Emil Vikström Avatar answered Oct 07 '22 00:10

Emil Vikström