Regex - cancel text after comma

If I have text:

test: firstString, blah: anotherString, blah:lastString

How can I get the text "firstString"

My regex is:

 test:(.*),

EDIT What returns firstString, blah: anotherString,, but do I need to return the text "firstString"?

+4
source share
1 answer

Use a low-fat quantifier:

test:(.*?),

Or character class:

test:([^,]*),

Ignore comma:

test:([^,]*)

If you want to omit test:, you can also use the look:

(?<=test:\s)[^,]*

Since you are using this grok debugger , I managed to get this to work using the named capture group:

(?<test>(?<=test:\s)[^,]*)
+11
source

All Articles