Help with regex search-and-replace (Perl to Python)

Tim Chase python.list at tim.thechases.com
Sun Feb 7 08:11:10 EST 2010


Schif Schaf wrote:
> On Feb 7, 12:19 am, "Alf P. Steinbach" <al... at start.no> wrote:
>> I haven't used regexps in Python before, but what I did was (1) look in the
>> documentation,
[snip]
>> <code>
>> import re
>>
>> text = (
>>      "Lorem [ipsum] dolor sit amet, consectetur",
>>      "adipisicing elit, sed do eiusmod tempor",
>>      "incididunt ut [labore] et [dolore] magna aliqua."
>>      )
>>
>> withbracks = re.compile( r'\[(.+?)\]' )
>> for line in text:
>>      print( re.sub( withbracks, r'{\1}', line) )
>> </code>
> 
> Seems like there's magic happening here. There's the `withbracks`
> regex that applies itself to `line`. But then when `re.sub()` does the
> replacement operation, it appears to consult the `withbracks` regex on
> the most recent match it just had.

I suspect Alf's rustiness with regexps caused him to miss the 
simpler rendition of

   print withbacks.sub(r'{\1}', line)

And to answer those who are reaching for other non-regex (whether 
string translations or .replace(), or pyparsing) solutions, it 
depends on what you want to happen in pathological cases like

   s = """Dangling closing]
      with properly [[nested]] and
      complex [properly [nested] text]
      and [improperly [nested] text
      and with some text [straddling
      lines] and with
      dangling opening [brackets
      """
where you'll begin to see the differences.

-tkc







More information about the Python-list mailing list