正则表达式 - 在逗号字符后丢弃文本

use*_*834 5 regex logstash logstash-grok

如果我有文字:

test: firstString, blah: anotherString, blah:lastString
Run Code Online (Sandbox Code Playgroud)

如何获取文本"firstString"

我的正则表达式是:

 test:(.*),
Run Code Online (Sandbox Code Playgroud)

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

p.s*_*w.g 12

使用非贪婪量词:

test:(.*?),
Run Code Online (Sandbox Code Playgroud)

或者是一个角色类:

test:([^,]*),
Run Code Online (Sandbox Code Playgroud)

要忽略逗号:

test:([^,]*)
Run Code Online (Sandbox Code Playgroud)

如果您想省略它test:,您可以使用这样的后视:

(?<=test:\s)[^,]*
Run Code Online (Sandbox Code Playgroud)

由于您正在使用此grok调试器,因此我可以使用命名捕获组来实现此功能:

(?<test>(?<=test:\s)[^,]*)
Run Code Online (Sandbox Code Playgroud)