470,636 Members | 1,634 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,636 developers. It's quick & easy.

Regex question

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?
Tia,

\\ jonas galvez
// jonasgalvez.com

Jul 18 '05 #1
1 1013
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').
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by engwar1 | last post: by
2 posts views Thread by Tim Conner | last post: by
6 posts views Thread by Du Dang | last post: by
17 posts views Thread by clintonG | last post: by
5 posts views Thread by Chris | last post: by
6 posts views Thread by Martin Evans | last post: by
7 posts views Thread by Extremest | last post: by
6 posts views Thread by Phil Barber | last post: by
6 posts views Thread by | last post: by
???
reply views Thread by Stoney L | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.