[Tutor] Regular expressions - Ignoring linefeeds

Kent Johnson kent37 at tds.net
Fri Mar 2 19:10:38 CET 2007


doug shawhan wrote:
> I've been looking through various sites, but cannot find the magic 
> button that allows me to match a string with linefeeds
> I'd rather not strip out the linefeeds, then stick them back in. :-)
> 
> I'm attempting something that should be fairly simple:
> 
> snippy = re.compile('Hi there.*Bye there.')

For . to match a newline you have to compile with re.DOT_ALL:
snippy = re.compile('Hi there.*Bye there.', re.DOT_ALL)

> 
> s = '''Good gravy! Hi there.
> I'm some text someone
> wants to match.
> Bye there. See you around'''
> 
> yield = re.match (snippy)

re.match() only matches at the start of the string; use re.search()

Kent
> 
> Anything after the linefeed is not matched, yielding naught but pain.
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list