a n00b regex qestion

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Mon Dec 3 16:34:21 EST 2007


nuffnough at gmail.com a écrit :
> I am doing a string.replace   in a simple table generation app I wrote,
> and I can't figure out how to match whitespace with /s,

Hahem... Where did you get the idea that str.replace would work with 
regexps ?

"""
replace(...)
     S.replace (old, new[, count]) -> string

     Return a copy of string S with all occurrences of substring
     old replaced by new.  If the optional argument count is
     given, only the first count occurrences are replaced.
"""

See any mention of regexps here ?-)



>  so  I thought
> I would see if osmeone where would be kind enough to tell me what I am
> getting wrong.
> 
> 
> This works:
> 
> string = string.replace('<tr>\n      <th class="table">Field One</th>
> \n      <td>%FieldOneValue%</td>\n    </tr>', '')
> 
> 
> You can see I had to actually put in space characters and linefeeds
> exactly as they are in the string.

Indeed. That's consistent with the doc.

> I tried these this:
> 
> string = string.replace('<tr>\s*<th class="table">Field One</th>\s*<td>
> %FieldOneValue%</td>\s*</tr>', '')
> 
> 
> But this doesn't work.

It works if you have *litteraly* the first arg in your string !-)

>  The doco for Python's regex suggests that \s
> should match any whitespace including newlines which is what I
> wanted, 

So why do you insist on using str.replace insted of re.sub ?-)

> but just in case,  I also tried this:
> 
> string = string.replace('<tr>\n\s*<th class="table">Field One</th>\n
> \s*<td>%FieldOneValue%</td>\n\s*</tr>', '')
> 
> 
> Any help explaining why these are not working would be greatly
> appreciated.

import re
pat =  r'<tr>\s*<th class="table">Field One</th>\s*<td>' \
      + r'%FieldOneValue%</td>\s*</tr>'
string = re.sub(pat, '', string)

HTH



More information about the Python-list mailing list