Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove content inside bracket without removing brackets

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()()
like image 929
Kongu B K Riot Avatar asked Feb 26 '21 06:02

Kongu B K Riot


People also ask

How do you remove content inside brackets without removing brackets in Python?

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.

How do I remove a string within a bracket in Python?

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.

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 remove brackets from text in Excel?

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.

How to remove content inside brackets without removing brackets in JavaScript?

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?

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.

How to remove all brackets from string in a range?

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 ...

How do I remove brackets in Excel?

Select the data range you want to remove brackets, and click Kutools > Text > Remove Characters.


1 Answers

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()()

like image 74
ΦXocę 웃 Пepeúpa ツ Avatar answered Oct 19 '22 00:10

ΦXocę 웃 Пepeúpa ツ