如何使用Perl在字符串中找到子字符串?

gag*_*eet 9 regex perl substring

我有一个字符串,我希望从中提取一个单词,但附加一个数字,每行可能不同:

This is string1 this is string
This is string11 
This is string6 and it is in this line
Run Code Online (Sandbox Code Playgroud)

我想解析这个文件并获取"stringXXX"的值,从0到100开始

# suppose ABC.txt contains the above lines
FH1 = open "Abc.txt"; 
@abcFile = <FH1>;

foreach $line(@abcFile) {
    if ($pattern =~ s/string.(d{0}d{100});
        print $pattern;
Run Code Online (Sandbox Code Playgroud)

以上打印整行,我希望只获得stringXXX

Nat*_*man 13

你需要抓住它:

while ($pattern =~/(string(100|\d{1,2}))/g) {
    print $1;
}
Run Code Online (Sandbox Code Playgroud)

说明:

  • 括号将其中的内容捕获到$ 1中.如果你有一组以上的parens,那么第一组将获得$ 1,第二组将获得$ 2等.在这种情况下,$ 2将拥有实际数字.
  • \ d {1,2}捕获1到3位数字,允许您捕获0到99之间.额外的100允许您明确捕获100,因为它是您想要匹配的唯一3位数字.

编辑:修复捕获的数字的顺序.


jfs*_*jfs 5

Abc.pl:

#!/usr/bin/perl -w    
while(<>) {
    while (/(string(\d{1,3}))/g) {      
    print "$1\n" if $2 <= 100;
    } 
}
Run Code Online (Sandbox Code Playgroud)

例:

$ cat Abc.txt 
This is string1 this is string
This is string11 
This is string6 and it is in this line
string1 asdfa string2
string101 string3 string100 string1000
string9999 string001 string0001

$ perl Abc.pl Abc.txt
string1
string11
string6
string1
string2
string3
string100
string100
string001
string000

$ perl -nE"say $1 while /(string(?:100|\d{1,2}(?!\d)))/g" Abc.txt
string1
string11
string6
string1
string2
string3
string100
string100
Run Code Online (Sandbox Code Playgroud)

注意输出之间的差异.什么是可取的取决于您的需求.