1

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.

2
  • 1
    You sure you need RegEx for this? Sounds like something that str.replace can do. Also, you should avoid shadowing built-in names like input. The reference to the input function will be overwritten by your list. Commented Oct 28, 2022 at 11:37
  • Actually I didn't ask question properly my bad..I have edited it Commented Oct 28, 2022 at 11:51

4 Answers 4

3

Try this:

input = ["dog,cat,21311 asd", "131 MAN"]
print([data.replace(' ', ',') for data in input])
Sign up to request clarification or add additional context in comments.

1 Comment

Actually, I only want to replace a space with comma for particular pattern only like "1554 NAS" or "78412 abcdef" not all spaces.
1

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.

Comments

1

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)

2 Comments

While this is technically correct and an answer to OP question, it does feel a little like cracking a walnut with a sledgehammer. In the same vein, the raw string literal for the space r" " seems a little unnecessary.
please help me with this issue.
1

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']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.