Python Forum

Full Version: replacing a run of spaces with a space bounded run of dots
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i would like to replace a run of 3 or more spaces with the same number of characters having a space on each end and a period character between them repeating as many times (N-2) so that it replaces the run of spaces without changing the length.
Output:
"foo bar" -> "foo ... bar" "x y" -> "x ....... y"
i probably want it to do this for every such run in a given string.
Output:
"a b c d" -> "a .. b ... c .... d"
is there a clean, short, pythonic way to do this? maybe with re?
(Oct-20-2019, 02:29 AM)Skaperen Wrote: [ -> ]i would like to replace a run of 3 or more spaces with the same number of characters having a space on each end and a period character between them repeating as many times (N-2) so that it replaces the run of spaces without changing the length.
Output:
"foo bar" -> "foo ... bar" "x y" -> "x ....... y"
i probably want it to do this for every such run in a given string.
Output:
"a b c d" -> "a .. b ... c .... d"
is there a clean, short, pythonic way to do this? maybe with re?
Hi!

I started to separate the steps I would need to get this done, so given, let's say 6 strings:
string11 = "foo     bar"
string12 = "fooXXXXXbar"
string21 = "x         y"
string22 = "xXXXXXXXXXy"
string31 = "a    b     c      d"
string32 = "aXXXXbXXXXXcXXXXXXd"
I would first calculate the length of the strings in units of character, I mean the length in characters of the strings, with something like this:

print(len(string11))
print(len(string12))
print(len(string21))
print(len(string22))
print(len(string31))
print(len(string32))
that gives me this output:
Output:
11 11 11 11 19 19
Now, I thought about splitting the strings into substrings with the separator ' ':

print(string11.split(' '))
print(string12.split(' '))
print(string21.split(' '))
print(string22.split(' '))
print(string31.split(' '))
print(string32.split(' '))
that gives me a curious output:
Output:
['foo', '', '', '', '', 'bar'] ['fooXXXXXbar'] ['x', '', '', '', '', '', '', '', '', 'y'] ['xXXXXXXXXXy'] ['a', '', '', '', 'b', '', '', '', '', 'c', '', '', '', '', '', 'd'] ['aXXXXbXXXXXcXXXXXXd']
that means that if you count them manually, this output shows the lengths of the strings as:

10
11
10
11
16
19

That is to say, that when the strings contain 'spaces', it seems that it gets short of one 'space' character for each substring of 'spaces'.

I was thinking that the next step could be substituting these substrings of 'spaces' by other ones according to your requirements (something like subtracting the total length of all the substrings -except the one dealt each time- from the length of the original string) and then joining all the substrings to form new ones complying with your requirements, but as you have just seen, the issue of correcting the number of characters in the substrings with 'spaces' has to be dealt previously to that.

These are just some thoughts about how to approach that problem ...

All the best,
Here is another way
>>> def spam(match):
...     return " {} ".format("." * (len(match.group()) - 2))
... 
>>> import re
>>> s = "a    b     c      d"
>>> re.sub(r'   +', spam, s)
'a .. b ... c .... d'
i figured re might have a solution. i don't exactly understand how 3 spaces match 4 spaces or 5 spaces. or does the + only apply to a single character?
Skaperen Wrote:i don't exactly understand how 3 spaces match 4 spaces or 5 spaces.
It is very easy to understand. The above regular expression's semantic is
Output:
<one space character> <one space character> <one or more space characters>
Equivalently, I could have written
r" {3,}"
which means
Output:
<three or more space characters>