[Tutor] Regular expressions - Ignoring linefeeds

Luke Paireepinart rabidpoobear at gmail.com
Fri Mar 2 19:09:33 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.')
Your RE is wrong.
Refer to http://docs.python.org/lib/re-syntax.html
* Causes the resulting RE to match 0 or more repetitions of the 
preceding RE, as many repetitions as are possible. ab* will match 'a', 
'ab', or 'a' followed by any number of 'b's.
>
> s = '''Good gravy! Hi there.
> I'm some text someone
> wants to match.
> Bye there. See you around'''
>
> yield = re.match (snippy)
There are multiple errors in this call.
First, yield is a reserved keyword in python 2.4+ (probably earlier, too.)
Second, re.match takes 2 arguments, not 1.
What you really want to do is re.match(snippy,s)
>
> Anything after the linefeed is not matched, yielding naught but pain.
Nothing is matched because the code doesn't work.
HTH,
-Luke


More information about the Tutor mailing list