如果我有文字:
test: firstString, blah: anotherString, blah:lastString
我怎样才能得到文本“firstString”
我的正则表达式是:
test:(.*),
编辑
这又带回来
firstString, blah: anotherString,
但我只需要带回文本“firstString”?
最佳答案
使用非贪婪量词:
test:(.*?),
或者一个字符类:
test:([^,]*),
也忽略逗号:
test:([^,]*)
如果您想省略
test:
你也可以像这样使用后视:(?<=test:\s)[^,]*
由于您正在使用此 grok debugger ,我能够通过使用命名的捕获组来使其工作:
(?<test>(?<=test:\s)[^,]*)
关于正则表达式 - 在逗号字符后丢弃文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20357176/