Regular expression question -- exclude substring

Kent Johnson kent37 at tds.net
Mon Nov 7 19:13:31 EST 2005


dreamerbin at gmail.com wrote:
> Hi,
> 
> I'm having trouble extracting substrings using regular expression. Here
> is my problem:
> 
> Want to find the substring that is immediately before a given
> substring. For example: from
> "00 noise1 01 noise2 00 target 01 target_mark",
> want to get
> "00 target 01"
> which is before
> "target_mark".
> My regular expression
> "(00.*?01) target_mark"
> will extract
> "00 noise1 01 noise2 00 target 01".

If there is a character that can't appear in the bit between the numbers then use everything-but-that instead of . - for example if spaces can only appear as you show them, use
"(00 [^ ]* 01) target_mark" or
"(00 \S* 01) target_mark"

Kent



More information about the Python-list mailing list