Home > Enterprise >  How to replace spaces between words using regex?
How to replace spaces between words using regex?

Time:07-05

I am trying to convert a string of words and numbers into a list, every item is separated with a space, so using .replace(" ", ",").split(",") would be an easy solution, but unfortunately, sometimes there are multiple words in the object name, and I would like these words to be connected with a _

Example:

office supplies 674.56 570.980487 755.84 682.360029

Expected output:

office_supplies 674.56 570.980487 755.84 682.360029

I have found this: Replace spaces between letters only

And tried to implement it like this:

sample_line = "office supplies 674.56 570.980487 755.84 682.360029"
regex = re.compile(':%s/\v(\a)\s(\a)/\1_\2/g', re.I)
print(re.sub(p, r"\1\2", line))

But it does not seem to replace the spaces, I am not very sharp with regex, but according to the linked issue, it should work.

CodePudding user response:

You may probably use this re.sub split solution:

import re
s = 'office supplies 674.56 570.980487 755.84 682.360029'
print ( re.sub(r'(?<=[a-zA-Z])\s (?=[a-zA-Z])', '_', s).split() )

Output:

['office_supplies', '674.56', '570.980487', '755.84', '682.360029']

Here:

  • Regex (?<=[a-zA-Z])\s (?=[a-zA-Z]) matches 1 whitespace surrounded with letters only
  • split will split string on whitespaces

CodePudding user response:

x=r'office supplies 674.56 570.980487 755.84 682.360029'
lead="_".join(x.split()[:2])
trail=" ".join(x.split()[2:])

expected_string = lead   " "   trail
print(expected_string)
  • Related