开发者

Regexp for extracting data in parenthesis and commas

So, i have this :

"( ABC,2004 )"

And I would need to extract ABC in a variable and 2004 in another. So what I have fo开发者_Go百科r now is this:

In: re.compile(r'([^)]*,').findall("( ABC,2004 )")

Out: ['( ABC,']


If your inputs are always like that (begin with "( ", end with " )"), you can have your values as:

input_text.strip(" ()").split(",")

>>> "( ABC,2004 )".strip(" ()").split(",")
['ABC', '2004']

This will consume any parentheses at the edges inside the outer parentheses.

Also, if the commas can be surrounded/succeeded by spaces, you can:

[item.strip() for item in input_text.strip(" ()").split(",")]


Try just looking for "word" characters:

>> re.compile(r'\w+').findall("( ABC,2004 )")
['ABC', '2004']
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜