9

I have text string like:

"abcd[e]yth[ac]ytwec"

I need just

"abcdythytwec"

What is the easiest way to do it using regex or otherwise in python? I am using .split('[') method which is cumbersome.

2
  • Is this a more general case? Is it possible there are more than two [e]'s? Commented Feb 27, 2012 at 18:29
  • Definitely. There may even be none. Commented Feb 27, 2012 at 18:31

2 Answers 2

18
In [11]: re.sub(r'\[.*?\]', '', 'abcd[e]yth[ac]ytwec')
Out[11]: 'abcdythytwec'
Sign up to request clarification or add additional context in comments.

Comments

2

Try using re module:

import re

re.sub(r'\[[^]]*\]', '', "abcd[e]yth[ac]ytwec")

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.