3

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?

Community
  • 1
  • 1
Pythonic
  • 2,091
  • 3
  • 21
  • 34

4 Answers4

2

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'
Mazdak
  • 105,000
  • 18
  • 159
  • 188
1

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
Mohammad Yusuf
  • 16,554
  • 10
  • 50
  • 78
0

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

das-g
  • 9,718
  • 4
  • 38
  • 80
Rahul K P
  • 15,740
  • 4
  • 35
  • 52
0

Relating from this post,

re.sub("\\s+(?=[^()]*\\))", "", my_string)
Community
  • 1
  • 1
Pythonic
  • 2,091
  • 3
  • 21
  • 34