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

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


Steve Holden wrote:
> Tim Chase wrote:
>> 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.
>>
> Really? Under what circumstances does a simple one-for-one character
> replacement operation fail?

Failure is only defined in the clarified context of what the OP 
wants :)  Replacement operations only fail if the OP's desired 
output from the above mess doesn't change *all* of the ]/[ 
characters, but only those with some form of parity (nested or 
otherwise).  But if the OP *does* want all of the ]/[ characters 
replaced regardless of contextual nature, then yes, replace is a 
much better solution than regexps.

-tkc





More information about the Python-list mailing list