Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing String Right Before Comma in Regex

Tags:

regex

capture

I am writing some raw Regex code and testing them on online testers. I want to capture a list of strings right before a comma. Specifically, I want to capture up to 3 strings right before a comma. Ex.

string string string,

I want to capture "string string string" (including spaces).

How do I do that?

like image 751
Concerned_Citizen Avatar asked Nov 29 '22 09:11

Concerned_Citizen


1 Answers

The safest way to capture the characters before the comma is: ^([^,]*)

Explanation:

^        Start of string
(        Start of capture group
[^,]*    Any number of any non-comma characters
)        End of capture group
like image 192
MRAB Avatar answered Dec 05 '22 16:12

MRAB