0

I have the following string:

'Cc1cc([N+](=O)[O-])ccc1OCC(C)(O)CN1CCN(Cc2ccccc2)CC1'

and want to capture [N+] and [O-], that is, splitting and recovering them. I do not seem to be able to recover them by using re.split.

re.split(r'\[[^\]]*\]','Cc1cc([N+](=O)[O-])ccc1OCC(C)(O)CN1CCN(Cc2ccccc2)CC1')

output:
['Cc1cc(', '(=O)', ')ccc1OCC(C)(O)CN1CCN(Cc2ccccc2)CC1']

and I am looking for something like this:

['Cc1cc(', '[N+]','(=O)','[O-]', ')ccc1OCC(C)(O)CN1CCN(Cc2ccccc2)CC1']

I am aware of edits like: Splitting on regex without removing delimiters or In Python, how do I split a string and keep the separators?

2
  • You can use: re.findall(r'\[([^\]]*)\]','Cc1cc([N+](=O)[O-])ccc1OCC(C)(O)CN1CCN(Cc2ccccc2)CC1') Commented Dec 3, 2019 at 14:39
  • Use a capturing group \[([^\]]*)\] and match instead of splitting Commented Dec 3, 2019 at 14:39

1 Answer 1

1

If you apply the function re.split wrapping your function with parenthesis you get the desired output:

s = 'Cc1cc([N+](=O)[O-])ccc1OCC(C)(O)CN1CCN(Cc2ccccc2)CC1'

re.split('(\[[^\]]*\])',s)

output : 
['Cc1cc(', '[N+]', '(=O)', '[O-]', ')ccc1OCC(C)(O)CN1CCN(Cc2ccccc2)CC1']
Sign up to request clarification or add additional context in comments.

2 Comments

I get this: ['Cc1cc(', '[N+]', '(=O)', '[O-]', ')ccc1OCC(C)(O)CN1CCN(Cc2ccccc2)CC1']
You might add a line or two of explanation, and show the actual result.

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.