[Tutor] regular expression question
Alexandre Ratti
alex at gabuzomeu.net
Sun Aug 31 16:36:55 EDT 2003
Hello,
nihilo wrote:
> I'm stuck on a regular expression. I want to match everything
> starting from a word up to and including the next occurence of the word.
If you are trying to match text between instances of a specific word,
you can try using "non-greedy" forms so that the expression matches as
little text as possible (i.e. only up to the 2nd instance and no
further). Example:
>>> import re
>>> s = "Nobody expects the FNORD Spanish Inquisition FNORD and they
have FNORD nice uniforms."
>>> pattern = re.compile("FNORD.*?FNORD")
>>> pattern.findall(s)
['FNORD Spanish Inquisition FNORD']
The question mark in ".*?" says it's the non-greedy form. For more
details, see:
http://www.python.org/doc/current/lib/re-syntax.html
If you haven't found it yet, see also the Regular Expression Howto:
http://www.amk.ca/python/howto/regex/regex.html
If the text between word instances can contain new lines, you'll have to
add "re.DOTALL" when compiling the expression.
Cheers.
Alexandre
More information about the Tutor
mailing list