Matching two overlapping patterns with Perl

前端 未结 2 1967
夕颜
夕颜 2021-01-05 08:15

I hope that my question has not already been posed by someone else, since I tried to look almost everywhere in the site but I couldn\'t manage to find an answer.

My

2条回答
  •  梦毁少年i
    2021-01-05 08:31

    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
    

提交回复
热议问题