Help: Arbitrary number of groups in regex

Duncan Booth duncan at NOSPAMrcp.co.uk
Fri Aug 9 10:16:18 CEST 2002


"Jean-Philippe Côté" <cotej at crt.umontreal.ca> wrote in 
news:AqC49.430$WJ.135248 at news20.bellglobal.com:

> I'm starting to think it's impossible too. Perhaps I oversimplified
> the problem in my. What I have is actually an arbitrary number
> or comma separated values, each of which can be
> composed of letters or numbers. I don't know the number in
> advance. For instance, I might have the following input:
> 
> " FL234,  MK434,  9743"
> 
> I've tried to write a regex pattern which could return me each value
> in a separate group, but I believe I have to 'split' the string first
> and then parse each value separately.
> 

Why on earth do you want to use a regular expression here at all? Isn't 
splitting on comma then stripping leading/trailing spaces sufficient:

>>> aString = " FL234,  MK434,  9743"
>>> groups = [s.strip() for s in aString.split(',')]
>>> print groups
['FL234', 'MK434', '9743']

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list