Tutorial :How to match a variable list of items separated by commas


I want to turn something like this

CS 240, CS 246, ECE 222, ... (more or less); Software Engineering students only  


('CS 240', 'CS 246', 'ECE 222', 'ECE 220')  

in Python, code that matches a single course looks like

>>> re.search('([A-Z]{2,5} \d{3})', 'SE 112').groups()  ('SE 112',)  

I prefer a regular expression only method because I have a bunch of other alternate reg exps using '|' to combine them. However, a method with split is acceptable.


>>> a="CS 240, CS 246, ECE 222"  >>> b=tuple(a.strip() for a in a.split(','))  >>> b  ('CS 240', 'CS 246', 'ECE 222')  >>>   


Isn't the csv standard library module ( http://docs.python.org/library/csv.html ) what you are looking for?


This method uses regular expressions and matches your input:

>>> import re  >>> re.findall("\w+\s\d+", "CS 240, CS 246, ECE 222")  ['CS 240', 'CS 246', 'ECE 222']  

It does not look for the comma. Instead it looks for anything but the comma: it first matches multiple word characters, then a space character, then multiple digits. Findall looks for all occurrences of this pattern.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »