tags:

views:

87

answers:

2

So, i have this :

"( ABC,2004 )"

And I would need to extract ABC in a variable and 2004 in another. So what I have for now is this:

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

Out: ['( ABC,']

+2  A: 

Try just looking for "word" characters:

>> re.compile(r'\w+').findall("( ABC,2004 )")
['ABC', '2004']
Stephen
+4  A: 

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(",")]
ΤΖΩΤΖΙΟΥ
This is a simple, elegant solution and... look ma, no regexp!
jathanism
Agreed. Though I love regexps (really) a large part of Python is knowing when not to use them.
rbp
as jathanism says, 'look ma, no regexp!' thank you TZ, its perfect
anders