string="file()(function)(hii)out(return)(byee)"
for this string i need output like
file()()()out()()
I have tried this
string="file()(function)(hii)out(return)(byee)"
string1=re.sub("[\(\[].*?[\)\]]", "", string)
string2=re.sub(r" ?\([^)]+\)", "", string)
print(string1)
print(string2)
and got output like
fileout
file()out
my desired output should look like this:
file()()()out()()
Method 1: We will use sub() method of re library (regular expressions). sub(): The functionality of sub() method is that it will find the specific pattern and replace it with some string. This method will find the substring which is present in the brackets or parenthesis and replace it with empty brackets.
The easiest way to get rid of brackets is with a regular expression search using the Python sub() function from the re module. We can easily define a regular expression which will search for bracket characters, and then using the sub() function, we will replace them with an empty string.
If you want to remove the [] and the () you can use this code: >>> import re >>> x = "This is a sentence.
Select a blank cell (says cell B2) for locating the result after removing brackets, enter formula =No_brackets(A2) into the Formula Bar, and then press the Enter key. Note: A2 is the cell contains the brackets you need to remove. 5.
We can remove content inside brackets without removing brackets in 2 methods, one of them is to use the inbuilt methods from the re library and the second method is to implement this functionality by iterating the string using a for loop Method 1: We will use sub () method of re library (regular expressions).
How to remove text inside brackets in Python? Import the re library. Now find the sub-string present in the brackets and replace with () using sub () method. We need to pass the sub () method with 2 arguments those are pattern and string to be replaced with. Print the string.
Remove all brackets from string in a range with Kutools for Excel 1 Select the range with brackets you need to remove, then click Kutools > Text > Remove Characters. See screenshot: 2 In the Remove Characters dialog box, check the Custom box, and enter the bracket marks into the textbox, then click... More ...
Select the data range you want to remove brackets, and click Kutools > Text > Remove Characters.
use regex: capture all between Parentheses, i.e. (.*?) and replace it with an empty string i.e. ( )
import re
x = "file()(function)(hii)out(return)(byee)"
x = re.sub("\(.*?\)", "()", x)
print(x)
this will print
file()()()out()()
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