2

Here is my code:

import re
string = r"('Option A' | 'Option B') & ('Option C' | 'Option D')"
word_list = re.split(r"[\(.\)]", string)
-> ['', "'Option A' | 'Option B'", ' & ', "'Option C' | 'Option D'", '']

I want the following result:

-> ["('Option A' | 'Option B')", ' & ', "('Option C' | 'Option D')"]
1
  • No. I tried this: "([(.)])", and got this: ['', '(', "'Option A' | 'Option B'", ')', ' & ', '(', "'Option C' | 'Option D'", ')', ''] I want brackets included in the string, not separate. Commented Aug 20, 2021 at 9:48

1 Answer 1

1

You can use re.findall to capture each parenthesis group:

import re
string = r"('Option A' | 'Option B') & ('Option C' | 'Option D')"
pattern = r"(\([^\)]+\))"
re.findall(pattern, string)
# ["('Option A' | 'Option B')", "('Option C' | 'Option D')"]

This also works with re.split

re.split(pattern, string)
# ['', "('Option A' | 'Option B')", ' & ', "('Option C' | 'Option D')", '']

If you want to remove empty elements from using re.split you can:

[s for s in re.split(pattern, string) if s]
# ["('Option A' | 'Option B')", ' & ', "('Option C' | 'Option D')"]

How the pattern works:

  • ( begin capture group
  • \( matches the character ( literally
  • [^\)]+ Match between one and unlimited characters that are not )
  • \) matches the character ) literally
  • ) end capture group
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer and guidelines on the use of patterns. Adding line word_list.remove('') next to re.split(pattern, string) eliminated empty elements. Please update it in your answer as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.