Nick Crawford Evolution and more…

30Apr/090

RegEx to id comma-space(s) or space(s) delimited text

I'm not all that great at RegEx, but I needed split a line of text on commas followed by spaces and/or by spaces (including tabs). 30 minutes later after swearing and sweating with iPython, I produced the following little expression. Who needs the CSV module?

 
re.compile(',(?:\s*)|\s*')
 

Example Usage:

  1. line = 'one, two three four, five'
  2. pattern = re.compile(',(?:\s*)|\s*')
  3. line.pattern(split)
  4. >>> ['one', 'two', 'three', 'four', 'five']
Filed under: Blog Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

(required)

No trackbacks yet.