我有文件的内容为:
(0872) "ss_current" (1 of 1)
(0873) "ss_current_6oct" (0 of 1)
Run Code Online (Sandbox Code Playgroud)
我想读取每行文件,然后获取最后一个括号之间的内容,即
(1 of 1)
(0 of 1)
Run Code Online (Sandbox Code Playgroud)
并且如果它们相等则比较数字,即"of"之前和之后的数字相等.我的代码:
my @cs;
while (<$fh>) {
if ($_ =~ /\((.*?)\)/) {
my $temp = $1;
print $temp, "\n";
}
}
Run Code Online (Sandbox Code Playgroud)
但这给出的内容为0872或0873
你的正则表达式只是拿起第一组括号.使它更具体,你可以选择(1 of 1)或(0 of 1):
while (<$fh>) {
# \d+ means match one or more adjacent numbers
# brackets capture the match in $1 and $2
if ($_ =~ /\((\d+) of (\d+)\)/) {
if ($1 == $2) {
# they are equal! print out the line (or do whatever)
# (the line is set to the special variable $_ while processing the file)
print "$_";
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
81 次 |
| 最近记录: |