Regular expressions

Peter Otten __peter__ at web.de
Wed Nov 4 03:57:02 EST 2015


Michael Torrie wrote:

> On 11/03/2015 08:23 PM, Steven D'Aprano wrote:
>>>> Grep can use regular expressions (and I do so with it regularly), but
>>>> it's default mode is certainly not regular expressions ...
>>>
>>> Its very name indicates that its default mode most certainly is regular
>>> expressions.
>> 
>> I don't even know what grep stands for.
>> 
>> But I think what Michael may mean is that if you "grep foo", no regex
>> magic takes place since "foo" contains no metacharacters.
> 
> More likely I just don't know what I'm talking about.  I must have been
> thinking about something else (shell globbing perhaps).
> 
> Certainly most of the times I've seen grep used, it's to look for a word
> with no special metacharacters, as you say. Still a valid RE of course.
>  But I have learned to night I don't need to resort to grep -e to use
> regular expressions.  At least with GNU grep, that's the default.

Well, I didn't know that grep uses regular expressions by default.

I tried Tim's example

$ seq 5 | grep '1*'
1
2
3
4
5
$ 

which surprised me because I remembered that there usually weren't any 
matching lines when I invoked grep instead of egrep by mistake. So I tried 
another one

$ seq 5 | grep '[1-3]+'
$ 

and then headed for the man page. Apparently there is a subset called "basic 
regular expressions":

"""
  Basic vs Extended Regular Expressions
       In basic regular expressions the meta-characters ?, +, {, |, (,
       and ) lose their special meaning; instead use  the  backslashed
       versions \?, \+, \{, \|, \(, and \).
"""





More information about the Python-list mailing list