Regex find 4th comma from end of line

I need a regular expression to match the fourth comma from the end of the line, my line ends with a comma.

For example, I would like to select a comma after G in the line below:

A,B,C,D,E,F,G,H,I,J,
+5
source share
2 answers

You can do this using lookahead:

,(?=(?:[^,]*,){3}[^,]*$)

See how it works on the Internet: Rubular

+5
source

You can use a quantifier and then return:

Single line input option (No newline)

/.*\K,(?=(?:[^,]+,){3})/

Single line matching version: (new lines displayed)

/.*\K,(?=(?:[^,\n]+,){3})/

Multi Line Compatibility:

/.*\K,(?=(?:[^,]+,){3})/s
0
source

All Articles