Regexp用于在括号和逗号中提取数据

and*_*ers 3 python regex

所以,我有这个:

"(ABC,2004)"

我需要在变量中提取ABC,在另一个中提取2004.所以我现在拥有的是:

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

出:['(ABC,']

tzo*_*zot 5

如果您的输入始终如此(以"(",以")结尾"),您可以将您的值设为:

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

>>> "( ABC,2004 )".strip(" ()").split(",")
['ABC', '2004']
Run Code Online (Sandbox Code Playgroud)

这将在边缘处消耗任何括号的外括号.

此外,如果逗号可以用空格包围/替换,您可以:

[item.strip() for item in input_text.strip(" ()").split(",")]
Run Code Online (Sandbox Code Playgroud)