[Tutor] regular expression question

Bob Gailer bgailer at alum.rpi.edu
Mon Sep 1 20:29:31 EDT 2003


At 09:46 AM 8/31/2003 +0000, nihilo wrote:

>hi,
>
>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 the word were a single character (say 'a'), then I could do the following:
>
>pat = re.compile('a[^a]*a')
>
>The problem is that I haven't been able to find out how to exclude the 
>word if it is more than one character. [^word] excludes the individual 
>letters, not the word as a whole, and I tried grouping with braces and 
>parentheses  [^(word)], but these don't work.  I've checked many re 
>tutorials, and they all say how to exclude a single character from the set 
>of characters inside the brackets, but not how to exclude more than 1 
>character.

Instead of asking it to exclude the word just match from word to word:
re.findall(r'.*?(word.*?word).*', 'A word to the wise is word enough')
gives:
['word to the wise is word']

Bob Gailer
bgailer at alum.rpi.edu
303 442 2625
-------------- next part --------------

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.506 / Virus Database: 303 - Release Date: 8/1/2003


More information about the Tutor mailing list