[Python-ideas] Experiment: Adding "re" to string objects.
MRAB
python at mrabarnett.plus.com
Sun Jul 19 23:20:08 CEST 2009
Jan Kaliszewski wrote:
> Antoine Pitrou <solipsis at pitrou.net> wrote:
>
>> Georg Brandl <g.brandl at ...> writes:
>>>
>>> Nick Coghlan schrieb:
>>>
>>> > However, the idea of adding more convenience classes to the re module
>>> > may still have some merit. In particular, when checking against
>>> multiple
>>> > regexes, being able to do something like the following might be
>>> helpful:
>>> >
>>> > m = re.Matcher(s)
>>> > if m.match(r'whatever(.*)'):
>>> > print m.group(1)
>>> > elif m.match(r'something (.*) else(.*)'):
>>> > print m.group(2)
>>
>> -0.5. Right now, objects in the re module are constructed from a regular
>> expression pattern -- one of the reasons being that these patterns are
>> compiled
>> to bytecode form, and the objects help retain the bytecode. Having
>> another
>> object type constructed from the string-to-match is confusing.
>
> Maybe it should be limited to using compiled regexps, not strings?
>
The patterns are currently cached in a dict in the re module in case
they're wanted again.
>> Besides, keeping
>> some kind of internal state about the last matched pattern, only for
>> "convenience" purposes, isn't pretty either.
>
> But 'practicality beats purity' :)
>
Only if it feels Pythonic. :-)
More information about the Python-ideas
mailing list