Search code examples
pythonpython-re

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']


Can anybody tell me the re expression for splitting the string:

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

to a list:

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

using Python?


Solution

  • You could capture anything -- except an opening parenthesis -- after a closing parenthesis, trimmed:

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

    result will be:

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