Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex to find 4th comma from the end of line

I need a regular expression to match the forth comma from the end of the line, my line end in a comma.

For example, I'd like to select the comma after the G in the line below:

A,B,C,D,E,F,G,H,I,J,
like image 507
van Avatar asked Feb 08 '12 14:02

van


People also ask

How do I find commas in regex?

The 0-9 indicates characters 0 through 9, the comma , indicates comma, and the semicolon indicates a ; . The closing ] indicates the end of the character set.

Which regex matches the end of line?

End of String or Line: $ The $ anchor specifies that the preceding pattern must occur at the end of the input string, or before \n at the end of the input string. If you use $ with the RegexOptions. Multiline option, the match can also occur at the end of a line.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.


1 Answers

You can do this using a lookahead:

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

See it working online: Rubular

like image 122
Mark Byers Avatar answered Nov 04 '22 03:11

Mark Byers