[Tutor] What the difference between the two RE?

Steven D'Aprano steve at pearwood.info
Thu Feb 9 12:36:40 CET 2012


daedae11 wrote:
> import re
> re.match("^hello", "hello")
> re.match("hello", "hello")
> 
> Please give a string that matches RE "^hello" but does not match RE
> "hello", or matches RE "hello" but does not match RE "^hello".

re.match always matches the beginning of the string, so

     re.match("^hello", something)
     re.match("hello", something)

are identical.

re.search will search anywhere in the string, not just the beginning, so:

     re.search("^hello", something)

means "find the beginning of the string followed by h e l l o"

while

     re.search("hello", something)

means "find h e l l o anywhere in the string".



-- 
Steven


More information about the Tutor mailing list