Matching two overlapping patterns with Perl

一笑奈何 提交于 2019-12-04 06:06:27
Chris Charley

The following uses a zero-width assertion (I believe that's what it's called).

#!/usr/bin/perl
use strict;
use warnings;

$_ = "betalphabetabeta";

while (/(?=(alpha|beta))/g) {
    print $1, "\n"; 

Prints:

C:\Old_Data\perlp>perl t9.pl
beta
alpha
beta
beta

You have to use looakahead and count the number of matches

(?=beta|alpha)

Not tested in perl but should work

works here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!