我有一个字符串,我希望从中提取一个单词,但附加一个数字,每行可能不同:
This is string1 this is string This is string11 This is string6 and it is in this line
我想解析这个文件并获取"stringXXX"的值,从0到100开始
# suppose ABC.txt contains the above lines FH1 = open "Abc.txt"; @abcFile =; foreach $line(@abcFile) { if ($pattern =~ s/string.(d{0}d{100}); print $pattern;
以上打印整行,我希望只获得stringXXX
你需要抓住它:
while ($pattern =~/(string(100|\d{1,2}))/g) { print $1; }
说明:
括号将其中的内容捕获到$ 1中.如果你有一组以上的parens,那么第一组将获得$ 1,第二组将获得$ 2等.在这种情况下,$ 2将拥有实际数字.
\ d {1,2}捕获1到3位数字,允许您捕获0到99之间.额外的100允许您明确捕获100,因为它是您想要匹配的唯一3位数字.
编辑:修复捕获的数字的顺序.
Abc.pl:
#!/usr/bin/perl -w while(<>) { while (/(string(\d{1,3}))/g) { print "$1\n" if $2 <= 100; } }
例:
$ 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
注意输出之间的差异.什么是可取的取决于您的需求.