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)
说明:
编辑:修复捕获的数字的顺序.
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)
注意输出之间的差异.什么是可取的取决于您的需求.
归档时间: |
|
查看次数: |
33417 次 |
最近记录: |