How to write this regular expression?

Jeremy Bowers jerf at jerf.org
Wed May 4 21:40:14 EDT 2005


On Thu, 05 May 2005 09:30:21 +0800, could ildg wrote:
> Jeremy Bowers wrote:
>> Python 2.3.5 (#1, Mar  3 2005, 17:32:12) [GCC 3.4.3  (Gentoo Linux
>> 3.4.3, ssp-3.4.3-0, pie-8.7.6.6)] on linux2 Type "help", "copyright",
>> "credits" or "license" for more information.
>> >>> import re
>> >>> m = re.compile("\d+")
>> >>> m.findall("344mmm555m1111")
>> ['344', '555', '1111']
>>
>> (I just tried to capture the three numbers by adding a parentheses set
>> around the \d+ but it only gives me the first. I've never tried that
>> before; is there a way to get it to give me all of them? I don't think
>> so, so two REs may be required after all.)

> You can capture each number by using group, each group can have a name.

I think you missed out on what I meant:

Python 2.3.5 (#1, Mar  3 2005, 17:32:12) 
[GCC 3.4.3  (Gentoo Linux 3.4.3, ssp-3.4.3-0, pie-8.7.6.6)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> m = re.compile(r"((?P<name>\d+)_){1,3}")
>>> match = m.match("12_34_56_")
>>> match.groups("name")
('56_', '56')
>>> 

Can you also get 12 & 34 out of it? (Interesting, as the non-named groups
give you the *first* match....) 

I guess I've never wanted this because I usually end up using "findall"
instead, but I could still see this being useful... parsing a function
call, for instance, and getting a tuple of the arguments instead of all of
them at once to be broken up later could be useful.



More information about the Python-list mailing list