Home > Net >  Regex - Match everything after second comma (or comma and space)
Regex - Match everything after second comma (or comma and space)

Time:11-29

I looked at this link: Regex - Match everything after second occurence. I cannot seem to substitute the http:// for a comma, let alone a comma and space. This is a sample of a string that I am working with:

42: A: b41a2431, B: 7615239a, we, are(the champion 12 .)

I am looking to extract anything after the second ", "...

So, that would be:

we, are(the champion 12 .)

(If it helps, I will be doing this in Python.)

CodePudding user response:

The regex doesn't work for you since the case is a bit different
That regex assumes the strings starts with http(s)://
If all the test strings have 2 commas then the regex is very simple
.*?,.*?,(. ) *? means lazy (vs greedy) quantifier so it will not eat the next comma (since it fits the "any character" .)

you can see the demo here https://regex101.com/r/XjLOVz/1

CodePudding user response:

Can't you just use split() and join()?

string = "42: A: b41a2431, B: 7615239a, we, are(the champion 12 .)"
result = ", ".join(string.split(", ")[2:])
print(result)

Output:

'we, are(the champion 12 .)'
  • Related