I'm looking for a regex with gives me the last occurance of a point or a comma in a string (whichever one is the further back)
From what I googled myself I got this one ([,\.])(?!.*\1)
but this gives me two results per string which isnt what I wanted.
I hope somebody can help me as I'm struggeling for the correct keyword to google for.
Cheers and thank you very much in advance
CodePudding user response:
This pattern ([,\.])(?!.*\1)
matches the last comma or dot asserting not any of the 2 being present on the right anymore.
That can occur for both a dot and a comma so you could possibly get 2 matches.
If you want a single match, you can match one of them and assert no more occurrences of either one of them to the right using a negated character class [^,.\r\n]*
matching any char except the listed characters.
Note that you don't need the capture group for a match only.
[,.](?=[^,.\r\n]*$)
See a regex demo.
CodePudding user response:
In this regex pattern the first capturing group is the last comma or point in each line.
We achieve this by looking for a comma or point and not allowing a point, comma or line ending between it and the end of the line.
([.,])[^.,\n\r]*$