Matching two overlapping patterns with Perl

前端 未结 2 1949
夕颜
夕颜 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条回答
  • 2021-01-05 08:19

    You have to use looakahead and count the number of matches

    (?=beta|alpha)
    

    Not tested in perl but should work

    works here

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题