[Python-Dev] Experiment: Adding "re" to string objects.

Michael Foord fuzzyman at voidspace.org.uk
Sat Jul 18 13:21:11 CEST 2009


MRAB wrote:
> Sean Reifschneider wrote:
>> I'm mailing this to python-dev because I'd like feedback on the idea of
>> adding an "re" attribute to strings.  I'm not sure if it's a good 
>> idea or
>> not yet, but I figure it's worth discussion.  The module mentioned here
>> includes a class called "restr()" which allows you to play with "s.re".
>>
>> As some of you may recall, I'm not particularly fond of the recipe:
>>
>>    m = re.match(r'whatever(.*)', s)
>>    if m:
>>       m.group(1)
>>
>> The other morning I came up on the idea of adding an "re" to strings, so
>> you could do things like:
>>
>>    if s.re.match(r'whatever(.*)'):
>>       print s.re.group(1)
>>
>> or:
>>
>>    if (date.re.match(r'(?P<year>\d\d\d\d)-(?P<month>\d\d)' or
>>          date.re.match(r'(?P<month>\d\d)-(?P<year>\d\d\d\d)'):
>>       print date.re.groupdict('year')
>>
> [snip]
> Why not drop the ".re" part? You would, however, then need a new name
> for the re split, eg "re_split".
>
> Or you could make the string the pattern, eg r'whatever(.*)'.match(s).

+1 for re support built-in to strings.

Michael Foord

> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: 
> http://mail.python.org/mailman/options/python-dev/fuzzyman%40voidspace.org.uk 
>


-- 
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/blog




More information about the Python-Dev mailing list