Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove text between () and []

I have a very long string of text with () and [] in it. I'm trying to remove the characters between the parentheses and brackets but I cannot figure out how.

The list is similar to this:

x = "This is a sentence. (once a day) [twice a day]" 

This list isn't what I'm working with but is very similar and a lot shorter.

like image 684
Tic Avatar asked Jan 30 '13 04:01

Tic


People also ask

How do I get rid of text between parentheses in Python?

If you want to remove the [] and the () you can use this code: >>> import re >>> x = "This is a sentence.

How do you delete text between two characters in Excel?

To eliminate text before a given character, type the character preceded by an asterisk (*char). To remove text after a certain character, type the character followed by an asterisk (char*). To delete a substring between two characters, type an asterisk surrounded by 2 characters (char*char).


1 Answers

You can use re.sub function.

>>> import re  >>> x = "This is a sentence. (once a day) [twice a day]" >>> re.sub("([\(\[]).*?([\)\]])", "\g<1>\g<2>", x) 'This is a sentence. () []' 

If you want to remove the [] and the () you can use this code:

>>> import re  >>> x = "This is a sentence. (once a day) [twice a day]" >>> re.sub("[\(\[].*?[\)\]]", "", x) 'This is a sentence.  ' 

Important: This code will not work with nested symbols

Explanation

The first regex groups ( or [ into group 1 (by surrounding it with parentheses) and ) or ] into group 2, matching these groups and all characters that come in between them. After matching, the matched portion is substituted with groups 1 and 2, leaving the final string with nothing inside the brackets. The second regex is self explanatory from this -> match everything and substitute with the empty string.

-- modified from comment by Ajay Thomas

like image 199
jvallver Avatar answered Sep 19 '22 14:09

jvallver