Python String Replace Character with Regex [Duplicate]

I've run into a regex problem I don't understand. I'm trying to replace a comma in between strings with a semi-colon and it's not working. Here's a sample string below. I set up a regex to replace everything but the content after "sequence" in a regex non-capture group, so that the comma on the end should be replaced with the only substring in the non-capture group, the semi-colon. But, it doesn't work. It only seems to preserve any of the string when I put (?:sequence:) as the non-capture groups. As soon as I add \d, it replaces the entire thing. I'm not sure why.

In my real problem, I have a serious of content tags marked with a colon and end with a semi-colon. In the tag sequence, there's a mistaken comma instead of semi-colon which I need to replace but leave everything unchanged. So, the solution should just change sequence:2, to sequence:2;

a_string = "tag1: content1 is this tag2: 0.1 amount; tag3: july 2020; sequence:2, tag4: content4"
new_string = re.sub(r"(?:sequence\:)(?:\d)(\,)", ";", a_string)

new_string

I looked at other solutions that should work, but don't for this. Any help is appreciated and please let me know if I can make this question any more clear.

0

You probably intended to use a positive negative lookbehind here:

a_string = "tag1: content1 is this tag2: 0.1 amount; tag3: july 2020; sequence:2, tag4: content4"
new_string = re.sub(r"(?<=\bsequence:\d)(\,)", ";", a_string)

print(new_string)

This prints:

tag1: content1 is this tag2: 0.1 amount; tag3: july 2020; sequence:2; tag4: content4

By the way, if you want to match the sequence text before the target comma directly, that's fine, but then replace it as well using a capture group:

a_string = "tag1: content1 is this tag2: 0.1 amount; tag3: july 2020; sequence:2, tag4: content4"
new_string = re.sub(r"(sequence:\d)(\,)", "\\1;", a_string)
print(new_string)   # same as above
2
David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest