Python Forum
replacing a run of spaces with a space bounded run of dots
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
replacing a run of spaces with a space bounded run of dots
#1
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
(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,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#3
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'
Reply
#4
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
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>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  parse/read from file seperated by dots giovanne 5 1,103 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  from global space to local space Skaperen 4 2,304 Sep-08-2020, 04:59 PM
Last Post: Skaperen
  Please support regex for version number (digits and dots) from a string Tecuma 4 3,161 Aug-17-2020, 09:59 AM
Last Post: Tecuma
  search and replace dots inside curly braces kchinnam 1 2,665 May-01-2018, 06:53 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020