Help: Arbitrary number of groups in regex

Sean 'Shaleh' Perry shalehperry at attbi.com
Thu Aug 8 19:51:24 EDT 2002


>> > Does anybody know what the PATTERN should be ?
>>
>> I don't believe it's possible.  Perhaps it should be.  If all you want
>> to do is split a string into a sequence of characters, just do this:
>>
>> >>> tuple("abcde")
>> ('a', 'b', 'c', 'd', 'e')
>>
> 
> 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.
> 

yeah it would be easiest if you split on commas and then parsed the individual
entries.

>>> re.split(r'[\s,]', " FL234,  MK434,  9743")
['', 'FL234', '', '', 'MK434', '', '', '9743']

you just have to skip the empties.

split is nice if the input is fairly uniform whereas findall can pull pieces
out of a larger mass.

If you use string.split(',', input) here the pieces will have spaces in them
which you will have to deal with.  That is why the regex I made breaks them off
as well.





More information about the Python-list mailing list