经过多日的谷歌搜索,最终我在这里发布问题,并希望通过专家们的帮助解决;我正在寻找可以匹配递增反向引用的正则表达式模式。让我解释一下:对于数字9422512322,模式(\d)\1将匹配22两次,我想要的模式(类似于(\d)\1+1)将匹配12(第二个数字等于第一个数字+1)。简而言之,该模式应匹配所有出现的数字对,如12、23、34、45、56等......没有替换,只需要匹配。
/01|12|23|34|45|56|67|78|89/
它可能不太引人注目,但它能做好它的工作。
(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9))+.
这将匹配:
1
的0
,或者2
的1
,或者3
的2
,...多次+
,然后匹配相应的字符.
。
这里有一个正则表达式演示, 匹配结果为:
12345555567877785
您可以在Perl正则表达式中运行代码,以控制正则表达式的执行流程。但是,在其他地方实现这种程度的功能不太可能。
PCRE具有一些程序变量交互,但不像Perl那样。(注意-要进行重叠查找,请将第二个( \d )
替换为(?=( \d ))
,然后将打印语句更改为print "Overlap Found $1$3\n";
)
如果您使用Perl,您可以进行各种数学字符关系,这是无法通过暴力排列组合完成的。
- 祝您好运!
Perl示例:
use strict;
use warnings;
my $dig2;
while ( "9342251232288 6709090156" =~
/
(
( \d )
(?{ $dig2 = $^N + 1 })
( \d )
(?(?{
$dig2 != $^N
})
(?!)
)
)
/xg )
{
print "Found $1\n";
}
输出:
Found 34
Found 12
Found 67
Found 01
Found 56
#!/usr/bin/env perl
use strict;
use warnings;
my $number = "9422512322";
my @matches = $number =~ /(0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9))/g;
# We have only matched the first digit of each pair of digits.
# Add "1" to the first digit to generate the complete pair.
foreach my $first (@matches) {
my $pair = $first . ($first + 1);
print "Found: $pair\n";
}
输出:
Found: 12
Found: 23