5

如果我有文字:

test: firstString, blah: anotherString, blah:lastString

我怎样才能得到文本“firstString”

我的正则表达式是:

 test:(.*),

编辑 哪个带回来firstString, blah: anotherString,但我只需要带回文本'firstString'?

4

1 回答 1

12

使用非贪婪量词:

test:(.*?),

或者一个字符类:

test:([^,]*),

忽略逗号:

test:([^,]*)

如果您也想省略 ,则test:可以使用这样的后视功能:

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

由于您使用的是这个grok debugger,我能够通过使用命名捕获组来使其工作:

(?<test>(?<=test:\s)[^,]*)
于 2013-12-03T16:59:51.160 回答