[Tutor] Regular expressions - Ignoring linefeeds
Jerry Hill
malaclypse2 at gmail.com
Fri Mar 2 19:07:50 CET 2007
On 3/2/07, doug shawhan <doug.shawhan at gmail.com> 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. :-)
Try this:
>>> s = '''Good gravy! Hi there.
I'm some text someone
wants to match.
Bye there. See you around'''
>>> snippy = re.compile('Hi there.*Bye there.', re.DOTALL)
>>> yeild = re.search(snippy, s)
>>> yeild.group()
"Hi there.\nI'm some text someone\nwants to match.\nBye there."
The important bits are compiling your regular expression with
re.DOTALL so that a dot will match newlines, and using re.search
instead of re.match.
--
Jerry
More information about the Tutor
mailing list