Capture multiple matches from the same Perl regex?

社会主义新天地 提交于 2021-02-16 15:41:07

问题


I have a file containing text:

hello mayank1 kumar mayank21
yadav Kevin has at most 
K
K minutes to perform this operations. He decides mayank3 that the string is super mayank4
if it is the lexicographically 
smallest among all possible strings mayank15
that he can get. Your task is to help Kevin and 
find this mayank2 lexicographically mayank8 smallest string mayank9

How can i find all mayank<number> ?

I tried:

use strict;

open( FH, "testfile.txt" ) or die "Can't open file for reading.";
while ( my $line = <FH> ) {
    chomp($line);
    while ( $line =~ /(mayank.*?)/g ) {
        print "$1\n";
    }
}

which is giving:

mayank
mayank
mayank
mayank
mayank
mayank
mayank
mayank

When using:

while ($line =~ /(mayank.?)/g) {
    print "$1\n";
}

I get

mayank1
mayank2
mayank3
mayank4
mayank1
mayank2
mayank8
mayank9

Please suggest.


回答1:


If you want to capture mayank followed by a number, you can use the following regex :

while ($line =~ /(mayank\d*)/g) {
    print "$1\n";
}

If the number is mandatory, change it to /(mayank\d+)/.

Short explanation : \d matches a single digit, so \d* matches as many digits as possible (or zero if there is none), and \d+ matches as many digit as possible (but at least one).

Why your solutions didn't work :

/(mayank.*?)/ uses a non-greedy quantifier (*?), which try to match at little characters as possible, so nothing.
/(mayank.?)/ will capture whatever character comes after mayank (even a space), if there is one.




回答2:


You want to capture mayank#id where id is a number or nothing:

$line =~ /(mayank)(\d+)?/
  • $1 : will hold mayank
  • $2 : will hold the id or will be empty (undef)

You may find more information about regular expression with Perl reading the manual:

man perlre


来源:https://stackoverflow.com/questions/39435914/capture-multiple-matches-from-the-same-perl-regex

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