Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove string between 2 characters from text string

Tags:

python

regex

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.

like image 623
jerrymouse Avatar asked Feb 27 '12 18:02

jerrymouse


People also ask

How do I extract a string between two characters in Excel?

To extract part string between two different characters, you can do as this: Select a cell which you will place the result, type this formula =MID(LEFT(A1,FIND(">",A1)-1),FIND("<",A1)+1,LEN(A1)), and press Enter key. Note: A1 is the text cell, > and < are the two characters you want to extract string between.

How do I extract part of a text string?

To extract a substring from the middle of a text string, you need to identify the position of the marker right before and after the substring. For example, in the example below, to get the domain name without the .com part, the marker would be @ (which is right before the domain name) and . (which is right after it).

How do I remove two characters from a string?

Use the String. substring() method to remove the last 2 characters from a string, e.g. const removedLast2 = str.


1 Answers

In [11]: re.sub(r'\[.*?\]', '', 'abcd[e]yth[ac]ytwec')
Out[11]: 'abcdythytwec'
like image 61
NPE Avatar answered Sep 26 '22 18:09

NPE