谁能告诉我将字符串拆分的 re 表达式-->('(A) gre (B)Toefl (C)PET (D)CET') 到列表 ['gre','Toefl', 'PET','CET']

can anybody tell me the re expression for split the string-->('(A) gre (B)Toefl (C)PET (D)CET') to a list ['gre','Toefl','PET','CET']

谁能告诉我拆分字符串的 re 表达式:

('(A) gre (B)Toefl (C)PET (D)CET')

到列表:

['gre','Toefl','PET','CET']

使用 Python?

我会在这里使用 re.findall 方法:

inp = "(A) gre (B)Toefl (C)PET (D)CET"
matches = re.findall(r'\([A-Z]+\)\s*(\w+)', inp)
print(matches)  # ['gre', 'Toefl', 'PET', 'CET']

您可以捕获任何内容 - 除了左括号 - 在右括号之后,修剪:

s = '(A) an example (B) next point (C)CAPS, lowercase! (D)CET'
result = re.findall(r"\)\s*([^(]*)(?<! )", s)

result 将是:

['an example', 'next point', 'CAPS, lowercase!', 'CET']