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

Anthra Norell anthra.norell at bluewin.ch
Mon Feb 8 03:30:51 EST 2010


Schif Schaf wrote:
> On Feb 7, 8:57 am, Tim Chase <python.l... at tim.thechases.com> wrote:
>   
>> Steve Holden wrote:
>>
>>     
>>> 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.
>>
>>     
>
> I need to do the usual "pipe text through and do various search/
> replace" thing fairly often. The above case of having to replace
> brackets with braces is only one example. Simple string methods run
> out of steam pretty quickly and much of my work relies on using
> regular expressions. Yes, I try to keep focused on simplicity, and
> often regexes are the simplest solution for my day-to-day needs.
>   
Could you post a complex case? It's a kindness to your helpers to 
simplify your case, but if the simplification doesn't cover the full 
scope of your problem you can't expect the suggestions to cover it.

Frederic




More information about the Python-list mailing list