In the string
my_string = 'abcd (ef gh ) ij'
I need to remove the spaces only when they appear within parentheses, resulting in:
my_clean_string = 'abcd (efgh) ij'
This post suggests how to remove all parentheses text entirely via re.sub(r'\\([^)]*\\)', '', my_string)
, however I do not know how specify the removal should only be applied to whitespaces ' '
.
Is there a regexpr (or simple python) solution that does this without looping through each character expressly?
Here is one general way that will work for nested parenthesis as well:
In [27]: my_string = 'abcd ( ()e(e w )f ) gh'
In [28]: re.sub(r' \(\s+|\s+\)', lambda x: x.group().strip(), my_string)
Out[28]: 'abcd(()e(e w)f) gh'
If you want to remove spaces even between words you can play around with look-arounds ;-):
In [40]: my_string = 'abcd ( ()e(e w )f ) gh'
In [41]: re.sub(r'\s+(?=[^[\(]*\))|((?<=\()\s+)', '', my_string)
Out[41]: 'abcd (()e(ew)f) gh'
This will remove spaces in front and behind the words inside the parenthesis.
import re
my_string = 'abcd ( ef dfg dfg ) gh'
print re.sub('\(\s*(.*?)\s*\)', lambda x: ''.join(x.group().split()), my_string, re.DOTALL)
Output:
abcd (efdfgdfg) gh
a solution without using regex,
my_string = 'abcd (ef ) gh'
str_to_replace = my_string[my_string.find('(')+1:my_string.find(')')]
out = my_string.replace(str_to_replace,str_to_replace.replace(' ',''))
Result
abcd (ef) gh
关于这篇文章 ,
re.sub("\\s+(?=[^()]*\\))", "", my_string)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.