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

2010-11-26 Thread Aahz
In article , Phlip wrote: > >Thanks all for playing! And as usual I forgot a critical detail: > >I'm writing a matcher for a Morelia /viridis/ Scenario step, so the >matcher must be a single regexp. Why? (You're apparently the author of Morelia, but I don't really understand it.) -- Aahz (a...

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

2010-11-25 Thread MRAB
On 25/11/2010 19:57, Phlip wrote: Accepting input from a human is fraught with dangers and edge cases. Here's a non-regex solution Thanks all for playing! And as usual I forgot a critical detail: I'm writing a matcher for a Morelia /viridis/ Scenario step, so the matcher must be a single re

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

2010-11-25 Thread Phlip
> Accepting input from a human is fraught with dangers and edge cases. > Here's a non-regex solution Thanks all for playing! And as usual I forgot a critical detail: I'm writing a matcher for a Morelia /viridis/ Scenario step, so the matcher must be a single regexp. http://c2.com/cgi/wiki?Mor

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

2010-11-25 Thread MRAB
On 25/11/2010 04:46, Phlip wrote: 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.

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

2010-11-25 Thread Steve Holden
On 11/24/2010 10:46 PM, Phlip wrote: > 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 wor

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

2010-11-25 Thread python
Phlip, > I'm trying to match "any word \w+ followed by a comma, or a final word > preceded by and." Here's a non-regex solution that handles multi-word values and multiple instances of 'and' (as pointed out by Alice). The posted code could be simplified via list comprehension - I chose the more

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

2010-11-25 Thread Alice Bevan–McGregor
Now that I think about it, and can be stripped using a callback function as the 'normalize' argument to my KeywordProcessor class: def normalize(value): value = value.strip() if value.startswith("and"): value = value[3:] return value parser = KeywordProcessor(',', normalize=no

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

2010-11-25 Thread Alice Bevan–McGregor
Accepting input from a human is frought with dangers and edge cases. ;) Some time ago I wrote a regular expression generator that creates regexen that can parse arbitrarily delimited text, supports quoting (to avoid accidentally separating two elements that should be treated as one), and work

Re: a regexp riddle: re.search(r'

2010-11-25 Thread Yingjie Lan
--- On Thu, 11/25/10, Phlip wrote: > From: Phlip > Subject: a regexp riddle: re.search(r' > To: python-list@python.org > Date: Thursday, November 25, 2010, 8:46 AM > HypoNt: > > I need to turn a human-readable list into a list(): > >    print re.search(r'(

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

2010-11-24 Thread Phlip
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',