Remove Text Between () and []

Remove text between () and []

This should work for parentheses. Regular expressions will "consume" the text it has matched so it won't work for nested parentheses.

import re
regex = re.compile(".*?\((.*?)\)")
result = re.findall(regex, mystring)

or this would find one set of parentheses, simply loop to find more:

start = mystring.find("(")
end = mystring.find(")")
if start != -1 and end != -1:
result = mystring[start+1:end]

Remove text between () and [] based on condition in Python?

You can use a simple regex with re.sub and a function as replacement to check the length of the match:

import re
out = re.sub('\(.*?\)|\[.*?\]',
lambda m: '' if len(m.group())<=(4+2) else m.group()[1:-1],
text)

Output:

'This is a sentence.  Once a day twice a day '

This give you the logic for more complex checks, in which case you might want to define a named function rather than a lambda

How to remove text between multiple pairs of brackets?

You have two main possibilities:

  • change .* to .*? i.e. match as few as possible and thus match ) as early as possible:

    text = Regex.Replace(text, @"\(.*?\)", "");
    text = Regex.Replace(text, @"\s{2,}", " "); // let's exclude trivial replaces
  • change .* to [^)]* i.e. match any symbols except ):

    text = Regex.Replace(text, @"\([^)]*\)", "");
    text = Regex.Replace(text, @"\s{2,}", " ");

How to remove all text between the outer parentheses in a string?

re matches are eager so they try to match as much text as possible, for the simple test case you mention just let the regex run:

>>> re.sub(r'\(.*\)', '', 'stuff(remove(me))')
'stuff'

How to remove text between two specific words in a dataframe column by python

You need to use Series.str.replace directly:

df['textcol'] = df['textcol'].str.replace(r'(?s)Original.*?Subject', '', regex=True)

Here, (?s) stands for re.DOTALL / re.S in order not to have to import re, it is their inline modifier version. The .*? matches any zero or more chars, as few as possible.

If Original and Subject need to be passed as variables containing literal text, do not forget about re.escape:

import re
# ... etc. ...
start = "Original"
end = "Subject"
df['textcol'] = df['textcol'].str.replace(fr'(?s){re.escape(start)}.*?{re.escape(end)}', '', regex=True)

Remove text between square brackets at the end of string

Note that \[.*?\]$ won't work as it will match the first [ (because a regex engine processes the string from left to right), and then will match all the rest of the string up to the ] at its end. So, it will match [something][something2] in input[something][something2].

You may specify the end of string anchor and use [^\][]* (matching zero or more chars other than [ and ]) instead of .*?:

\[[^\][]*]$

See the JS demo:

console.log(   "input[something][something2]".replace(/\[[^\][]*]$/, ''));

Remove text between parentheses in dart/flutter

Hi You can use this RegExp

String str = "Test Message (To Be removed)";
var test = str.replaceAll(RegExp('\\(.*?\\)'), '');
print(test);

JavaScript/regex: Remove text between parentheses

"Hello, this is Mike (example)".replace(/ *\([^)]*\) */g, "");

Result:

"Hello, this is Mike"

Remove text between two square brackets in javascript

There is expression without regarding nested [[]] myString.replace(/\[\[[^\]]*\]\]/g,''); It is tricky thing to track nested including of pair of symbols



Related Topics



Leave a reply



Submit