Why regular expressions are required
By Wang Yin
At the beginning of learning Unix, we all learned about regular expressions (regexp). But has anyone considered why we need regular expressions?
The regular expression originally intended to be used to extract information from the unstructured string, but this is precisely the UNIX flaw. UNIX represents data with unstructured strings, leading to the emergence of many complex regexp-based software. SED, AWK, Perl, ... All came to this world for the same purpose. If it were not for UNIX to represent data in strings, we would have direct storage by data structure type without the need to toss regexp. The regular expression has its own value (for natural language), but we don't actually need to apply it to the program language and the operating system.
The regular expression itself is represented by a string, which leads to other problems. Because the essence of a regular expression is not a string, but a data structure. People who have studied computational theory may know that this data structure is called NFA (nondeterministic finite automaton, non-deterministic finite automaton). All data structures should be represented by the programming language itself, just as it is used to construct an object in Java new ClassA("a") . But the regular expression forces you to write this simple constructor call as a string. So in this analogy, you have to write new ClassA(\"a\") . This way, when you want to combine these expressions, you will find that regular expressions are almost non-combinable (compose). It is almost impossible to regexp the two variables a and B securely into one, such as a Java string splicing a+b. Because you don't know what the cross-reactions of these strange symbols will be after the two strings are spelled together, the final recognition is not what you want.
In regular expressions, because the constructor of the regular expression itself is merged with the data itself, we have to escape certain "special characters". These special characters, in fact, are used to describe the NFA's tokens, which belong to a higher level of language. In regular expressions, however, they are confused with the characters in the NFA node. For example, a very simple block comment regular expression, but to write this way:
/\\*([^\\*]| [^/]) *\\*/
Obviously such expressions are prone to error. If we construct this expression in a programming language expression, it should look like this:
(@... "/*" (@*(@!" */")) "*/" )
In this scheme expression of my own design, the identifiers that @ begin with are constructors. Among them is the construction of @... sequence, @* is constructs a zero-or-more match, @! constructs a negative match. This expression is said: "To / * begin with, then 0 or more characters are not * / , and then the last one * / ." In this way, it is clear that what expression is "layered" and that it does not require a backslash escape, and that such an expression can be compose. Like what:
(Define REG1 (@ ... "/*" (@*(@!" * *)) (define REG2 (@+ "foo")) (Define REG3 (@= "B"))
After defining these three expressions, we can then use (@... reg1 (@or reg2 reg3)) an expression like this to concatenate 3 different expressions to construct a larger expression. Such a construct can be infinitely extended. From here and past experience, I have summed up a universally applicable program design lesson: Try not to "compress" multiple levels of language into one layer. We also see a strong relationship between regular expressions and the "Unix Philosophy". I do not have archaeology, so I do not know the first and the other, but they certainly have a direct causal relationship. Both are sources of Unix complexity.
This article is posted at Yinwang ' s Sina blog,
On 2012-05-17.
Though it ' s not available now.
(GO) Why do I need regular expressions by Wang Yu