Regular exp matching delimited string excepting trailing delimiters?
Eddie Corns
eddie at holyrood.ed.ac.uk
Thu Oct 31 15:06:59 EST 2002
More information about the Python-list mailing list
Thu Oct 31 15:06:59 EST 2002
- Previous message (by thread): Regular exp matching delimited string excepting trailing delimiters?
- Next message (by thread): Regular exp matching delimited string excepting trailing delimiters?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Alex Martelli <aleax at aleax.it> writes: >Jeff Kowalczyk wrote: >> Can anyone suggest a reg exp (using re) that will match the entirety of a >> delimited string of values, omitting zero or more delimiters at the end of >> the string? For example: >> >> from 'A,B,C,123,D,E,,,,,,' with delimiter ',' match 'A,B,C,123,D,E' >> >> I have (,*\Z) to match the trailing delimiters for removal with string >> slicing, but I'd prefer to directly match the text to keep, or match both >> keep and discard as groups. What would be the syntax for an omission like >> that? Thanks. >Perhaps: > ([^,]+,?)* >but that won't work if you can have e.g. A,B,,,,D,E,Z,,, ("empty fields"). (.*[^,]),*$ ie everything up to a non ',' followed by as many commas as possible until the end. >>> x='A,B,,,,D,E,Z,,,' >>> m=re.search(r'(.*[^,]),*$',x) >>> m.groups() ('A,B,,,,D,E,Z',) Doesn't work with empty strings tho or just ,,, Eddie
- Previous message (by thread): Regular exp matching delimited string excepting trailing delimiters?
- Next message (by thread): Regular exp matching delimited string excepting trailing delimiters?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list