给定这个字符串:
Equinox: *spamReceiveTask: Mar 17 12:34:39.264: #CAPWAP-3-DTLS_CONN_ERR: capwap_ac.c:934 00:3a:9a:30:f5:90: DTLS connection not found forAP 192.168.99.74 (43456), Controller: 192.168.99.2 (5246) send packet
我想在Equinox和下一个冒号之间匹配所有的东西。所以我的对手应该是*垃圾邮件接收任务
我试过这个:^.*\bEquinox:([^:]+):
但它与Equinox: *spamReceiveTask:相匹配
我还尝试了“向前看”和“后面看”选项,但是它匹配字符串中所有分号之间的所有东西。我只想匹配所有字符之间的前两个冒号排除冒号。
发布于 2020-03-24 02:54:24
通过使用回顾和展望断言,请您尝试:
(?<=Equinox:)[^:]+(?=:)发布于 2020-03-24 00:39:43
你的大梁很好,而且还能用。我想问题是在语言特定的regex实用程序中,您可能是错误地使用它们;您想要的输出是在捕获组1中;
检查下面用javascript编写的代码片段,并在您的模式中使用匹配();
let sample = `Equinox: *spamReceiveTask: Mar 17 12:34:39.264: #CAPWAP-3-DTLS_CONN_ERR: capwap_ac.c:934 00:3a:9a:30:f5:90: DTLS connection not found forAP 192.168.99.74 (43456), Controller: 192.168.99.2 (5246) send packet`;
//only the first complete match and its related capturing groups are returned as an array
let pattern = /^.*\bEquinox:([^:]+):/
let matchAndCaptureGroup = sample.match(pattern);
let captureGroup = matchAndCaptureGroup[1];
console.log("without global flag: ", captureGroup);
//------------------------------------------
//with global flag, capturing group is not return
//only the matched strings will return
pattern = /^.*\bEquinox:([^:]+):/g
let matchOnly = sample.match(pattern)[0];
console.log("with global flag: ", matchOnly);
https://stackoverflow.com/questions/60823346
复制相似问题