0

Given string s = '(A /something_1)(B /something_2)(C /something_3),/,(D /something_4)(D /something_5)'

I would like to get this output: (C /something_3),/,(D /something_4)(D /something_5)

I keep matching the whole string s, instead of getting above substring.

I am using re.search(r'(\(C.*\)),/,(\(D.*\))+')

Any help is appreciated...

2 Answers 2

4

You're just about there - re.search(r'(\(C.*\)),/,(\(D.*\))+', s).group() will get you what you want.

>>> import re
>>> s = '(A /something_1)(B /something_2)(C /something_3),/,(D /something_4)(D /something_5)'
>>> re.search(r'(\(C.*\)),/,(\(D.*\))+', s).group()
'(C /something_3),/,(D /something_4)(D /something_5)'

Are you wanting to further split that in groups?

Sign up to request clarification or add additional context in comments.

Comments

1

Using Python 2.7, I get the exact result you're after:

import re
s = '(A /something_1)(B /something_2)(C /something_3),/,(D /something_4)(D /something_5)'
m = re.search(r'(\(C.*\)),/,(\(D.*\))+', s)

s[m.start():m.end()] == '(C /something_3),/,(D /something_4)(D /something_5)'

Comments

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.