Regex question

Luke luked at xplantechnology.com
Wed Jun 23 09:58:49 CEST 2004


Jonas Galvez wrote:
> I've a perhaps unusual piece of data to parse with regexes.
> 
> 
>>>>import re
>>>>re.findall("a (\w ?)*", "a b c d e f g h")
> 
> ['h']
> 
> This is a very very very simplified example. The result I was
> expecting is the following: ['a', 'b', 'c'] ... and so forth. Is it
> impossible to repeat a pattern group inside another?

Are you hoping for your regular expression to match the whole string, or 
just one letter?  This is how I would get the result you seek:

 >>> import re
 >>> re.findall("\w", "a b c d e f g h")
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

The regular expression you used, "a (\w ?)*", will match the whole string, 
which is why you have only one element in the list returned by findall. 
Since you used a group (using parentheses), findall only returned what was 
inside the group (the 'h') rather than the whole match ('a b c d e f g h').



More information about the Python-list mailing list