首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >捕捉两个冒号之间的所有信息的第一次匹配

捕捉两个冒号之间的所有信息的第一次匹配
EN

Stack Overflow用户
提问于 2020-03-24 00:20:37
回答 2查看 125关注 0票数 0

给定这个字符串:

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:相匹配

我还尝试了“向前看”和“后面看”选项,但是它匹配字符串中所有分号之间的所有东西。我只想匹配所有字符之间的前两个冒号排除冒号。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-03-24 02:54:24

通过使用回顾和展望断言,请您尝试:

代码语言:javascript
复制
(?<=Equinox:)[^:]+(?=:)
票数 1
EN

Stack Overflow用户

发布于 2020-03-24 00:39:43

你的大梁很好,而且还能用。我想问题是在语言特定的regex实用程序中,您可能是错误地使用它们;您想要的输出是在捕获组1中;

检查下面用javascript编写的代码片段,并在您的模式中使用匹配()

代码语言: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);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60823346

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档