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.
If you want to remove the [] and the () you can use this code: >>> import re >>> x = "This is a sentence.
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).
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With