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

Peter Otten __peter__ at web.de
Wed Sep 26 04:33:11 EDT 2012


iMath wrote:

> 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()  ?

Why not try it out in the interactive interpreter? Here's the "deluxe 
version":

>>> def demo(pattern="mid$", texts=["start mid end", "start mid", "mid end", 
"mid"], matchers=[re.match, re.search]):
...     print "pattern:", pattern
...     for text in texts:
...             for matcher in matchers:
...                     name = matcher.__name__
...                     print u"\N{CHECK MARK}" + name if matcher(pattern, 
text) else (" "*(len(name)+1)),
...             print repr(text)
... 
>>> demo()
pattern: mid$
               'start mid end'
       ✓search 'start mid'
               'mid end'
✓match ✓search 'mid'





More information about the Python-list mailing list