简体   繁体   English

如何匹配重叠的Perl正则表达式?

[英]How to match overlapping perl regex?

I am trying to get a regex to match an overlapping section when using /g. 我正在尝试使用/ g时获取正则表达式以匹配重叠部分。 I believe I need to use lookbehind, but I'm having trouble understanding the documentation and getting it to match. 我相信我需要使用向后看,但是我在理解文档并使其匹配方面遇到困难。

For example, the test case: 例如,测试用例:

use feature ':5.18';
use warnings;
use diagnostics;
use utf8;

my $test = '1 1 1';
$test =~ s/(?=[0-9]+) ([0-9]+)/$1$2/g;
say $test;      # still get '1 1 1'

How do I get rid of the spaces? 我如何摆脱空间? The output should be '111'. 输出应为“ 111”。

To be able to remove spaces between digits you can use zero-width look-arounds assertions : 为了能够删除数字之间的空格,您可以使用零宽度环顾断言

$test =~ s/(?<=[0-9])\s+(?=[0-9])//g;

Breakup: 分手:

  • (?<=[0-9]) : Assert that we have a digit at previous position (?<=[0-9]) :断言我们在前一个位置有一个数字
  • \\s+ : Match 1+ whitespaces \\s+ :匹配1+个空格
  • (?=[0-9]) : Assert that we have a digit at next position (?=[0-9]) :声明我们在下一个位置有一个数字

Problems are encountered when both adjacent digits are part of the match. 当两个相邻的数字都是匹配项时会遇到问题。

$test =~ s/([0-9])\s+([0-9])/$1$2/g;   # XXX Bad

Solutions: 解决方案:

$test =~ s/(?<=[0-9])\s+(?=[0-9])//g;

or the more efficient 或更有效

$test =~ s/[0-9]\K\s+(?=[0-9])//g;   # 5.10+

In the former, the adjacent digits are never part of the match. 在前者中,相邻的数字永远都不是匹配项的一部分。

In the latter, only the preceding digit is part of the match. 在后者中,只有前一位数字是匹配的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM