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

Nick Coghlan ncoghlan at gmail.com
Sat Jul 18 16:18:11 CEST 2009


<switching lists from python-dev to python-ideas>

Michael Foord wrote:
> MRAB wrote:
>> [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.

The idea of adding a mutable attribute to strings (even if it plays no
part in string equality) sends shivers down my spine. It also seems like
it would be difficult to do this in a way that didn't increase the size
of all strings by at least one pointer slot.

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)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------



More information about the Python-ideas mailing list