Help: Arbitrary number of groups in regex

Jean-Philippe Côté cotej at crt.umontreal.ca
Thu Aug 8 16:16:52 EDT 2002


I apologize if this a common and/or stupid question (it probably is),
but I can't figure it out.

I'm trying to write a regular expression pattern which can return
an arbitrary number of groups, depending on the string on
which is it applied.

For instance, if I do
>>> import re
>>> m = re.match("PATTERN", "abcde")
>>> m.groups()
I'd like to see
('a','b','c','d','e')

and if I do
>>> import re
>>> m = re.match("PATTERN", "xy")
>>> m.groups()
I'd like to see
('x','y')

but by using a single generic pattern, and not "(\w)(\w)(\w)(\w)(\w)" in the
first case and "(\w)(\w)" in the second case.

The way I undestand "(\w)*" is <<match a single alphanumeric
character, put in into a group, return that group and repeat as
long a you can>>, but that doesn't work:
>>> m = re.match("(\w)*", "abcde")
>>> m.groups()
('e',)
>>>

Does anybody know what the PATTERN should be ?

Thanks in advance,
J-P


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.381 / Virus Database: 214 - Release Date: 8/2/2002





More information about the Python-list mailing list