kdba
kdba

Reputation: 433

Selectively remove blank spaces in a string

I have a string in the following format:

'[        A A A A A          B B B B B B B B      A A A A A         ]'

I want the spaces before the start and after the end to be completely gone but the spaces in between to be reduced by half. So the output should look like:

'[AAAAA     BBBBBBBB   AAAAA]'

There is one blank space between each of the characters and that has to be removed. The space between individual words has to be reduced by half. In the above example, the space between A A A A and B B B B B B B B is 10 blank spaces. But in the output I need it to be AAAA and BBBBBBBB separated by 5 blank spaces.

Upvotes: 0

Views: 61

Answers (1)

Olivier Melançon
Olivier Melançon

Reputation: 22294

Using re.sub, you will need multiple regexp pattern to accomplish this. Here are the steps and the transformation applied to your string at each step.

import re

s = '[        A A A A A          B B B B B B B B      A A A A A         ]'

# Using a lookbehind, we remove spaces preceeded by a [
s = re.sub('(?<=\[) +', '', s)
# s: '[A A A A A          B B B B B B B B      A A A A A         ]'

# Using a lookahead, we remove spaces followed by a ]
s = re.sub(' +(?=\])', '', s)
# s: '[A A A A A          B B B B B B B B      A A A A A]'

# Using a lookaround, we remove space not followed or preceeded by another space
s = re.sub('(?<! ) (?! )', '', s)
# s: '[AAAAA          BBBBBBBB      AAAAA]'

# Finally, we use str.replace to cut out half of the spaces
s = s.replace('  ', ' ')

print(s) # '[AAAAA     BBBBBBBB   AAAAA]'

The above steps can be merged by using regexp union and chaining the re.sub and str.replace operations.

s = re.sub('((?<=\[) +)|( +(?=\]))|((?<! ) (?! ))', '', s).replace('  ', ' ')

Upvotes: 2

Related Questions