RegEx用于排除捕获组

时间:2019-05-14 17:59:24

标签: regex regex-group

我已经尝试了几个小时来为此设计一个正则表达式,但未能做到。

这是我正在搜索的行。我只需要提取[Name] Action Detail部分。

2019-05-14 11:28:08,257 [tomcat-http--12] INFO  com.my.org.SomeClass  - Usage Event: ,SomeField=null,SomeOtherField=Some value,Action=[Name] Action Detail,OtherField=null

此正则表达式几乎为我提供了我所需的东西:Action=\[[^,]+。但是,我需要排除Action=部分。我正在考虑这样做,所以我需要使用嵌套子组吗?

1 个答案:

答案 0 :(得分:3)

您只需在Action =周围添加一个捕获组即可,

(Action=)\[[^,]+

您还可以在所需的输出周围将其扩展到另一个捕获组,以简单地提取该内容:

(Action=)(\[[^,]+)

enter image description here

RegEx

您可以在regex101.com中设计/修改/更改表达式。

RegEx电路

您可以在jex.im中可视化您的表情:

enter image description here

JavaScript演示

const regex = /(Action=)(\[[^,]+)/gm;
const str = `2019-05-14 11:28:08,257 [tomcat-http--12] INFO  com.my.org.SomeClass  - Usage Event: ,SomeField=null,SomeOtherField=Some value,Action=[Name] Action Detail,OtherField=null`;
const subst = `$2`;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log('Substitution result: ', result);

相关问题