正则表达式:有界组中的匹配

时间:2018-07-16 17:17:42

标签: regex regex-lookarounds

我需要在预定标签和不确定标签之间匹配文本块中的回车符。

在这种情况下,边界标签为:

预定:X-Gmail-Labels:

不确定:(?:^[\w\-]+:),例如:Delivered-To:ABC123:

感谢WiktorStribiżew对this thread的回答,我对应该采取的解决方案有一个大概的了解。

我不确定如何应用我认为需要的东西:边界不确定标签的未捕获超前组。

简单地说,我想删除与X-Gmail-Labels:相关的文本中的所有回车符。如果可以匹配它们,就可以删除它们!

初始尝试的正则表达式:

(?:\bX-Gmail-Labels:|(?!^)\G)[^\r]*\K\r

样本数据:

From 1604610346950104244@xxx Fri Jun 29 12:34:35 +0000 2018
X-GM-THRID: 1604610346950104244
X-Gmail-Labels: Archived thing,Unread
Delivered-To: joe.schmoe@gmail.com
Received: by 2002:a9f:3005:0:0:0:0:0 with SMTP id h5-v6csp731836uab;
        Fri, 29 Jun 2018 05:34:36 -0700 (PDT)
From 1604610346950104244@xxx Fri Jun 29 12:34:35 +0000 2018
X-GM-THRID: 1604610346950104244
X-Gmail-Labels: Also Archived 
Day-of-week: Tuesday
Received: by 2002:a9f:3005:0:0:0:0:0 with SMTP id h5-v6csp731836uab;
        Fri, 29 Jun 2018 05:34:36 -0700 (PDT)
From 1604610346950104244@xxx Fri Jun 29 12:34:35 +0000 2018
X-GM-THRID: 1604610346950104244
X-Gmail-Labels: Archived 
thing,
Unread
Favorite-fruit: bananas
Received: by 2002:a9f:3005:0:0:0:0:0 with SMTP id h5-v6csp731836uab;
        Fri, 29 Jun 2018 05:34:36 -0700 (PDT)
From 1604610346950104244@xxx Fri Jun 29 12:34:35 +0000 2018
X-GM-THRID: 1604610346950104244
X-Gmail-Labels: Archived 
 ,Read
ABC123: DoReMe
Received: by 2002:a9f:3005:0:0:0:0:0 with SMTP id h5-v6csp731836uab;
        Fri, 29 Jun 2018 05:34:36 -0700 (PDT)
From 1604610346950104244@xxx Fri Jun 29 12:34:35 +0000 2018
X-GM-THRID: 1604610346950104244
X-Gmail-Labels: Archived 
thing,Unread
emais
Received: by 2002:a9f:3005:0:0:0:0:0 with SMTP id h5-v6csp731836uab;
        Fri, 29 Jun 2018 05:34:36 -0700 (PDT)

(?:^[\w\-]+:)

正则表达式applied to data上方显示不确定的标记模式。

(?:\bX-Gmail-Labels:\G)[^\r]*\K\r

正则表达式applied to data上方显示无限制的匹配项。

谢谢! -菲茨

0 个答案:

没有答案