re.search question

Hans Nowak wurmy at earthlink.net
Wed Jul 31 18:18:26 EDT 2002


michaelian ennis wrote:

> How would I tell python to give me all the text in a string which
> falls between  the first instance of "!" alone on a line and the first
> instance of "end" alone on a line inclusively?
> 
> 
> lines=
> junk 
> !
> important stuff
> end
> more junk
> 
> 
> to 
> 
> cleaned_lines=
> !
> important stuff
> end

 >>> s = """\
junk
!
important stuff
end
more junk
"""
 >>>
 >>> import re
 >>> re_spam = re.compile("^!$(.*?)^end$", re.DOTALL|re.MULTILINE)
 >>> m = re_spam.search(s)
 >>> m
<_sre.SRE_Match object at 0x00770FB8>
 >>> m.group(1)
'\nimportant stuff\n'

HTH,

-- 
Hans (base64.decodestring('d3VybXlAZWFydGhsaW5rLm5ldA=='))
# decode for email address ;-)
The Pythonic Quarter:: http://www.awaretek.com/nowak/




More information about the Python-list mailing list