用于排除捕获组的正则表达式

RegEx for excluding a capture 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= 部分。我正在考虑这样做我需要使用嵌套子组吗?

您可以简单地在 Action= 周围添加一个捕获组,它会这样做:

(Action=)\[[^,]+

您还可以围绕所需的输出使用另一个捕获组对其进行扩展,以简单地提取:

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

正则表达式

您可以 design/modify/change 在 regex101.com 中表达您的表情。

正则表达式电路

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

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 = ``;

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

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