Skip to content
Advertisement

Put comma after a pattern in python regex

Just like the question says I am trying to add a comma at the end of a pattern or sub string. I found 3 solutions that should do the job, and look logical too. But they are not changing anything. I will show you all those codes. The goal is to find out if there is something that I am missing or something I can add to make it work.

By the way, on Google most of these comma related questions are about adding comma in a number string at thousandth place, then 5th place etc. like this- 1,00,000. That is not what I am looking for.

So here are some of the codes I tried:

JavaScript

Here I tried reading it line by line from a text file. The content variable at the bottom showing you what is inside the text file. Those are just test strings. Each string has a pattern for date, followed by some text mixed with numbers then a floating point.

Ideally I would like to put a comma right after the date and another one after the text and number mix, just before the floating point number.

To keep it simple I started by adding just the first comma after the date.

The variable regex has the pattern for date. The variable rep_str containing what I want the pattern to be replaced with, that is the pattern followed by a comma. Then re.sub to do the job. The output is the strings in the text files with no change. No comma nothing.

Next code:

JavaScript

Even simpler, no text file, just 1 string. The code has 2 patterns. One for date, other one for everything else. Tried to add comma between them. Same result. No comma, no error, just the same string as output.

Third attempt:

JavaScript

This piece of code is collected from here. First part of the code looking for a alphanumeric that ends with a number with word boundaries. Next brace is confirming that there is not a comma already. Then place the comma. This code apparently solved a similar problem I have. So I gave it a try. Surprisingly the result has been the same. No error, no change, same string as output.

If you spot anything or can think of a working code, do advise.

Advertisement

Answer

You need to use

JavaScript

See the Python demo:

JavaScript

Output:

JavaScript

Details:

  • with open('pizza.txt', 'r') as f: – opens the pizza.txt file for reading
  • for line in f: – reads the f file line by line
  • print(re.sub(r'd{2}/d{2}/d{4}', r'g<0>,', line)) – prints the result of the regex subsitution: r'd{2}/d{2}/d{4}' (mind the single backslashes in the raw string literal) finds all occurrences of two digits, /, two digits, / and four digits and replaces with the same found value (the g<0> backreference refers to the whole match value) and appends a comma right after. `
Advertisement