Weiest
Weiest

Reputation: 58

Python Regex: Internal reoccurring spaces only

Given the following string on python:

    foo    bar

I've been attempting to remove the recurring spaces only between 'foo' and 'bar' and replacing it with only once space but leaving behind the first indentation/spaces.

    foo bar

I've tried the following regex with usually it ends up with unintentional results.

[\s]+     # which selects all spaces
foobar
[\w][\s]+ # which selects the first characters and following spaces
    fobar

Is there no way to accomplish this using regex?

Edited: Sorry! It might not have been clear but the string may vary as my main objective is to remove only reoccurring spaces with the sentences without affecting indents! Edited again: The difference between other question that has already been asked on StackOverflow is that I want the spaces from the beginning to remain instead of removing all spaces. This project was to read txt files with indents and remove all empty spaces within the txt file.

Upvotes: 1

Views: 48

Answers (1)

anubhava
anubhava

Reputation: 785611

You can use a look around regex:

>>> s = '    foo    bar    '
>>> print re.sub(r'(?<=\S)\s+(?=\S)', ' ', s)
    foo bar
  • (?<=\S): Lookbehind to assert that we have non-space before
  • \s+: Match 1+ whitespaces
  • (?=\S): Lookahead to assert that we have non-space ahead

Alternatively, you may also use capturing groups:

>>> print re.sub(r'(\S)\s+(\S)', r'\1 \2', s)
    foo bar

Upvotes: 4

Related Questions