regular expression : the dollar sign ($) work with re.match() or re.search() ?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Jan 7 04:54:24 EST 2013


On Mon, 07 Jan 2013 01:45:58 -0800, iMath wrote:

> 在 2012年9月26日星期三UTC+8下午3时38分50秒,iMath写道:
>> I only know  the dollar sign ($) will match a pattern from the
>> 
>> end of a string,but which method does it work with ,re.match() or
>> re.search()  ?
> 
> I thought re.match('h.$', 'hbxihi') will match ‘hi’ ,but it does not .so
> why ?


re.match only matches at the *start* of the string, so "h.$" tries to 
match:

* start of string
* literal h
* any character
* end of string


You want re.search, which will search the entire string and match "hi" at 
the end of the string.


-- 
Steven



More information about the Python-list mailing list