问题描述
在一个文件中查找某些日志时,我得到两种类型的日志(一种带有空格,一种不带空格).我现在想使用一个正则表达式从这些日志中提取 doSomething 和 doAnotherThing .
When looking for some logs in one file I got two types of logs (one with white spaces and one without).I would now like to extract doSomething and doAnotherThing out of these logs with one regular expression.
日志文件1:"taskType":"doSomething"
日志文件2:"taskType":"doAnotherThing"
我编写了这个正则表达式: taskType .....(?< taskType1> \ w +)
I coded this regular expression: taskType.....(?<taskType1>\w+)
它对日志文件1有效,但对日志文件2无效,因为它剪切了单词的前两个字符.有没有解决这个问题的方法?
It works good for Logfile 1 but not for Logfile 2, because it cuts the first two characters of the word. Is there a way to eliminate this issue?
谢谢!
推荐答案
"tasktype" \ s?:\ s?((doSomething | doAnotherThing)"
对我有用,请尝试此处 https://regex101.com/r/Mx1RtT/1
"taskType"\s?:\s?"(doSomething|doAnotherThing)"
works for me, try it here https://regex101.com/r/Mx1RtT/1
这篇关于如何修改正则表达式,以便提取两个字段的相同字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!