Replace spaces with commas using Regex in python

Question:

I want to replace space with comma for this particular pattern only not all spaces:
"21311 asd"

Input:

["dog,cat ax 21311 asd","131 MAN"]

Desired output:

["dog,cat ax 21311,asd","131,MAN"]

Code:

input = ["dog,cat ax 21311 asd","131 MAN"]

new_list = []
for i in input:
    word = re.findall(r"d*s[a-zA-Z]*" , i)
    new_word = re.sub(word , r"d*s[a-zA-Z]*" , i)
    new_list = new_list + new_word
print(new_list)

I know this is wrong syntax, but I don’t know how to do it using Regex or any other method.

I’m using Python 3 and Jupyter notebook.

Asked By: Rushabh Nalawade

||

Answers:

Try this:

input = ["dog,cat,21311 asd", "131 MAN"]
print([data.replace(' ', ',') for data in input])
Answered By: AlexanderKondrat

You are using re.sub() with the wrong argument positions, the correct way is to call it like re.sub(regex, replace, string). Also you are iterating over input incorrectly. That’s the right way to do it:

import re
input = ["dog,cat,21311 asd","131 MAN"]

new_list = []
for word in input:
    new_word = re.sub(r" " , ",", word)
    new_list.append(new_word)

print(new_list)
Answered By: jvx8ss

OK, now that you clarified your request, assuming the pattern you are interested in is

one or more digits followed by a space followed by one or more ASCII letters

Here is the correct way to do it:

import re

pattern = re.compile(r"(d+)s([a-zA-Z])")
replacement = r"1,2"

inp = ["dog,cat ax 21311 asd", "131 MAN"]

out = [re.sub(pattern, replacement, s) for s in inp]

print(out)

The re.sub function accepts references to matching groups in its repl argument. We group the digits (1) and the letters (2) and thus replace the substring with those two groups with a comma in between.

Answered By: Daniil Fajnberg

Solution with re.sub() and pattern with positive Lookahead and Lookbehind

import re
lst = ["dog,cat ax 21311 asd","131 MAN"]

new_list = [re.sub(r"(?<=d)s+(?=[a-zA-Z])",",", i) for i in lst]
print(new_list)
['dog,cat ax 21311,asd', '131,MAN']
Answered By: Алексей Р
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.