Help with regex search-and-replace (Perl to Python)
Steve Holden
steve at holdenweb.com
Sun Feb 7 08:37:07 EST 2010
Tim Chase wrote:
> 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.
>
Really? Under what circumstances does a simple one-for-one character
replacement operation fail?
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/
More information about the Python-list
mailing list