Regex - discard text after comma character
Asked Answered
F

1

5

If I have the text:

test: firstString, blah: anotherString, blah:lastString

How can I get the text "firstString"

My regex is:

 test:(.*),

EDIT Which brings back firstString, blah: anotherString, but I only need to bring back the text 'firstString'?

Fitzsimmons answered 3/12, 2013 at 16:58 Comment(0)
V
12

Use a non-greedy quantifier:

test:(.*?),

Or a character class:

test:([^,]*),

To ignore the comma as well:

test:([^,]*)

If you'd like to omit the test: as well you can use a look-behind like this:

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

Since you're using this grok debugger, I was able to get this to work by using a named capture group:

(?<test>(?<=test:\s)[^,]*)
Vaduz answered 3/12, 2013 at 16:59 Comment(7)
this is bring back the text "test: firstString" how can I get just the "firstString"?Fitzsimmons
@Fitzsimmons Just extract the first capture group: See docs.oracle.com/javase/7/docs/api/java/util/regex/…Vaduz
I'm not using the Java API for this one.Fitzsimmons
@Fitzsimmons I assumed since the question is tagged with Java, you were using the Java API. What are you using instead?Vaduz
@Fitzsimmons Well, I'm not familiar with that at all, but you could try using a look-behind: (?<=test:\s*)[^,]*Vaduz
This is the best i have which is very close, there is just a colon to omit: (?<test>(:([^,]*)))Fitzsimmons
@Fitzsimmons After playing around in the debugger, I got a pattern that worked. See my updated answer.Vaduz

© 2022 - 2024 — McMap. All rights reserved.