From: Klaus Stein Date: 2003-04-16T21:51:53+09:00 Subject: Re: regular expressions Daniel Carrera wrote: > I guess it all started with the shell's "wildcards", which were extended > by grep, awk and sed. Then came Perl which extended those again. > Each extension caused a slight change in syntax, and an increase in > functionality. Thus, you will find that they are all very similar, but > not quite identical (and each is a functional superset than its ancestor). It's little more difficult. First you shouldn't mix wildcards and regexp, they are different at all. To do regexps with the chars available in ASCII jou have to treat some chars special. So e.g. in grep the chars ``.'' and ``*'' (and some others) are special and have special meanings (as we all know). If you want to include one of this chars literally in a regexp, you escape it by ``\'': ``\*''. Other chars like ``('' did not have a special meaning and therefore there was no need to escape it. Now the feature of grouping was added to grep, and ``('' and ``)'' were choosen to start and end a group. This would need to change the meaning of ``('' and ``)'' from a literal to a special char, and this would break old scripts using ``('' and ``)'' literally. So the programmers of grep decided to use ``\('' and ``\)'' for grouping. With any new feature added the situation got more and more fucked up: some chars have special meaning without being escaped: ``. * [ ] ^ $'' ..., and have to get escaped to be used literally: ``\. \* \[ \] \^ \$'' ..., others don't have special meaning like ``( ) < > |'' ... and gain special meaning by being escaped: ``\( \) \< \> \|'', so you have to remember for a lot of chars whether they have to be escaped or not (and these chars differ in different tools, compare emacs regexps, egrep, grep, vi, awk, ...). So perl did a cut and decided to simplify (and ruby thankfully adopted): Any char within a-zA-Z0-9 (and maybe umlauts like ����) is taken literally, so ``S'' is a literal ``S'' and may gain special meaning by being escaped (like ``\S'' or ``\1''). Any other char ``# ' % & $ ^ ? = ) ( /'' and many more may have special meaning and have to be escaped by ``\'' if to be used literally. So you just have to remember 2 character groups, which is much easier (and even future extensions can be done much easier). The only thing you loose is compatibility with tools like grep. Stony (42) -- ====================================== The Answer is 42. And I am the Answer. Now I am looking for the Question. ======================================