[Tutor] 回复: Re: What the difference between the two RE?

Christian Witts cwitts at compuscan.co.za
Thu Feb 9 09:34:40 CET 2012


On 2012/02/09 10:17 AM, daedae11 wrote:
> So doesn't it means the follow two sentences can achieve the same goal?
> re.match("^hello", "hello")
> re.match("hello", "hello")
> ------------------------------------------------------------------------
> daedae11
> *发件人:* Christian Witts <mailto:cwitts at compuscan.co.za>
> *发送时间:* 2012-02-09 16:05
> *收件人:* daedae11 <mailto:daedae11 at 126.com>; tutor at python.org 
> <mailto:Tutor at python.org>
> *主题:* Re: [Tutor] What the difference between the two RE?
> On 2012/02/09 09:44 AM, daedae11 wrote:
>> However, re.match("hello", "And they said hello" ) will also return 
>> None. So "And they said hello" also won't be matched by the regex 
>> pattern "hello".
>> ------------------------------------------------------------------------
>> daedae11
>> *From:* Christian Witts <mailto:cwitts at compuscan.co.za>
>> *Date:* 2012-02-09 15:16
>> *To:* daedae11 <mailto:daedae11 at 126.com>
>> *CC:* turor_python <mailto:tutor at python.org>
>> *Subject:* Re: [Tutor] What the difference between the two RE?
>> On 2012/02/09 08:15 AM, 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".
>>> ------------------------------------------------------------------------
>>> daedae11
>>>
>>>
>>> _______________________________________________
>>> Tutor maillist  -Tutor at python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>> The caret ^ means "At the beginning of the line" so 'And they said 
>> hello' will not be matched by the regex pattern '^hello'.
>>
>> The docs are pretty good on the module
>> http://docs.python.org/library/re.html
>> http://docs.python.org/howto/regex.html
>> -- 
>>
>> Christian Witts
>> Python Developer
>> //
>>
>> --
> From the docs http://docs.python.org/library/re.html#re.match
> Note: If you want to locate a match anywhere in string, use search() 
> instead.
> -- 
>
> Christian Witts
> Python Developer
> //
>
If your use case is that simple the caret is not needed as .match only 
searches from the start of the string to see if the pattern is there. If 
you need to look anywhere in your string for your pattern you should be 
using .search instead which if you do you can put the caret in if you 
just want to match if it's at the start.

 >>> import re
 >>> re.match('hello', 'hello there')
<_sre.SRE_Match object at 0x02324528>
 >>> re.match('^hello', 'hello there')
<_sre.SRE_Match object at 0x023243D8>
 >>> re.match('^hello', 'hi, hello there')
 >>> re.match('hello', 'hi, hello there')
 >>> re.search('hello', 'hi, hello there')
<_sre.SRE_Match object at 0x02324528>
 >>> re.search('^hello', 'hi, hello there')
 >>>
-- 

Christian Witts
Python Developer
//
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120209/0efb4d14/attachment-0001.html>


More information about the Tutor mailing list