a regexp riddle: re.search(r'(?:(\w+), |and (\w+))+', 'whatever a, bbb, and c') =? ('a', 'bbb', 'c')

Phlip phlip2005 at gmail.com
Thu Nov 25 05:46:18 CET 2010


HypoNt:

I need to turn a human-readable list into a list():

   print re.search(r'(?:(\w+), |and (\w+))+', 'whatever a, bbb, and
c').groups()

That currently returns ('c',). I'm trying to match "any word \w+
followed by a comma, or a final word preceded by and."

The match returns 'a, bbb, and c', but the groups return ('bbb', 'c').
What do I type for .groups() to also get the 'a'?

Please go easy on me (and no RTFM!), because I have only been using
regular expressions for about 20 years...

--
  Phlip
  http://bit.ly/ZeekLand



More information about the Python-list mailing list