signals.Signals$MsgSig:
cmdResponse {
ap {
.....
req {
Id: 192
.......
}
res {
Status: Received
.........
Id: 192
.........
}
...
}
....
}
sysContainer
signals.Signals$MsgSig:
cmdResponse {
ap {
.....
req {
Id: 192
.......
}
res {
Status: VALID
.........
Id: 192
.........
}
...
}
....
}
sysContainer
signals.Signals$MsgSig:
cmdResponse {
ap {
.....
req {
Id: 193
.......
}
res {
Status: Received
.........
Id: 193
.........
}
...
}
....
}
sysContainer
signals.Signals$MsgSig:
cmdResponse {
ap {
.....
req {
Id: 193
.......
}
res {
Status: VALID
.........
Id: 193
.........
}
...
}
....
}
sysContainer
我正在尝试为搜索实现负向前瞻。 Signal从Signals $ MsgSig开始:以sysContainer结束。我需要根据IdValue和Status进行过滤。
Status = "VALID"
IdValue = "193"
aFile = File.read('C:\Users\Documents\Application.log')
aFile.match(/(?x)Signals\$MsgSig:\n((?!Signals\$MsgSig:).)*?
Id:\W+#{IdValue}\n((?!Signals\$MsgSig:).)*?sysContainer\s\{/m).to_s
这里我想获取一个特定的MsgSig:tat的Id值为192,状态为有效。在上面的正则表达式中我应该在哪里添加搜索“status”。
如果我添加如下所示的搜索,它将匹配第一个MsgSig和IdValue 192.
aFile.match(/(?x)Signals\$MsgSig:\n((?!Signals\$MsgSig:).)*?
Id:\W+#{IdValue}(.*?)autoProgramStatus:\W+#{apStatus}((?! Signals\$MsgSig:).)*?sysContainer\s\{/m).to_s
请让我知道我在这里做错了什么。
答案 0 :(得分:0)
我已经尝试过您的代码了,我认为您通过添加冗余\{
错误地使用了正则表达式。
aFile.match(/(?x)Signals\$MsgSig:\n((?!Signals\$MsgSig:).)*?Id:\W+#{IdValue}\n((?!Signals\$MsgSig:).)*?sysContainer\s/m).to_s
# => "Signals$MsgSig:\ncmdResponse {\nap {\nreq {\n Id: 192\n}\nres {\nStatus: Received\nId: 192\n}\n}\n}\nsysContainer\n"
p.s.1
我认为不需要使用否定前瞻,并且删除负前瞻部分代码也可以工作。
aFile.match(/(?x)Signals\$MsgSig:\n(.)*?Id:\W+#{IdValue}\n(.)*?sysContainer\s/m).to_s
# => "Signals$MsgSig:\ncmdResponse {\nap {\nreq {\n Id: 192\n}\nres {\nStatus: Received\nId: 192\n}\n}\n}\nsysContainer\n"
p.s.2
要自己解决这个问题,我会通过
来解决str = '' # the first block of code of your post
parts = str.scan(/signals\.Signals\$MsgSig:\n(.*?)\nsysContainer/m).flatten
# => [
"cmdResponse {\nap {\nreq {\n Id: 192\n}\nres {\nStatus: Received\nId: 192\n}\n}\n}",
"cmdResponse {\nap {\nreq {\nId: 192\n}\nres {\nStatus: VALID\nId: 192\n}\n}\n}",
"cmdResponse {\nap {\nreq {\n Id: 193\n}\nres {\nStatus: Received\nId: 193\n}\n}\n}",
"cmdResponse {\nap {\nreq {\nId: 193\n}\nres {\nStatus: VALID\nId: 193\n}\n}\n}"
]
parts.detect{|s| s.match /(.*)Id: 192(.*)Status: VALID(.*)/m }
# => "cmdResponse {\nap {\nreq {\nId: 192\n}\nres {\nStatus: VALID\nId: 192\n}\n}\n}"