Python's regular expression?

John Machin sjmachin at lexicon.net
Mon May 8 09:37:09 EDT 2006


On 8/05/2006 11:13 PM, Mirco Wahab wrote:
> Hi John
> 
>>>    import re
>>>
>>>    t = 'blue socks and red shoes'
>>>    p = re.compile('(blue|white|red)')
>>>    if p.match(t):
>> What do you expect when t == "green socks and red shoes"? Is it possible
>> that you mean to use search() rather than match()?
> 
> This is interesting.
> What's in this example the difference then between:

I suggest that you (a) read the description on the difference between 
search and match in the manual (b) try out search and match  on both 
your original string and the one I proposed.

> 
>    import re
> 
>    t = 'blue socks and red shoes'
>    if re.compile('blue|white|red').match(t):
>       print t
> 
> and
> 
>    t = 'blue socks and red shoes'
>    if re.search('blue|white|red', t):
>       print t
[snip]
> 
> But what would be an appropriate use
> of search() vs. match()? When to use what?

ReadTheFantasticManual :-)




More information about the Python-list mailing list