开发者

How to match a variable list of items separated by commas

I want to turn so开发者_JAVA百科mething like this

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

into

('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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜