Perl Regex-打印匹配的值
打印匹配值的perl正则表达式是什么?例如,我想从这个字符串打印Perl Regex-打印匹配的值,regex,perl,Regex,Perl,打印匹配值的perl正则表达式是什么?例如,我想从这个字符串打印195 "2011-04-11 00:39:28,736::[main]::INFO (Main.java:73) Test.Main::main() Total Successful Transactions = 195". 我该怎么做?提前感谢如是: if ("2011-04-11 00:39:28,736::[main]::INFO (Main.java:73) Test.Main::main() Total Success
195
"2011-04-11 00:39:28,736::[main]::INFO (Main.java:73) Test.Main::main() Total Successful Transactions = 195".
我该怎么做?提前感谢如是:
if ("2011-04-11 00:39:28,736::[main]::INFO (Main.java:73) Test.Main::main() Total Successful Transactions = 195" =~ /(\d+)$/) {
print $1;
}
匹配后,匹配的组将以$1..$n的形式提供。您可以使用正则表达式中的括号捕获子字符串。捕获的组存储在$1、$2中,依此类推。例如:
while (<>) {
if (/Total Successful Transactions = (\d+)/) {
print "$1\n";
}
}
它允许您编写一个紧凑的单行程序(除其他外,-l
选项会自动为每次打印添加新行):
195将存储在$1中检查
“
和
之前的最后一个数字
#!/usr/bin/env perl
use strict;
use warnings;
my $string = q{"2011-04-11 00:39:28,736::[main]::INFO (Main.java:73) Test.Main::main() Total Successful Transactions = 195".};
my ($number) = $string =~ m{ (\d+) " \. \z}x;
print "Matched number: $number\n" if defined $number;
那就:
print $string =~ /Total Successful Transactions = (\d+)/;
你很少真正需要使用$1
和朋友
/(\d+$)/;
print $1;
我想你只是想要号码
195
。对吧?只是想把前面的答案重新组织一下;注意在这种情况下:
$ perl -e '$str="the variable Xy = 3 in this case"; print $str =~ /Xy = 3/;'
1
…我们得到打印出来的1-如“是的,我得到了一个匹配项”。如果我们想返回匹配的文本部分,如前所述,“使用正则表达式中的括号捕获子字符串”:
但是,请注意,在捕获以下内容时,使用此习惯用法连接字符串可能会出现问题:
$ perl -e '$str="the variable Xy = 3 in this case"; print $str =~ /Xy = 3/ . "\n";'
1 # OK
$ perl -e '$str="the variable Xy = 3 in this case"; print $str =~ /(Xy = 3)/ . "\n";'
1 # NOT OK
…因此,在这种情况下,最好将事情分开:
$ perl -e '$str="the variable Xy = 3 in this case"; print $str =~ /(Xy = 3)/ ; print "\n";'
Xy = 3 # OK again
但是,如果我们想捕获“某物”,比如一个数字,那么,由于我们使用括号,我们会自动返回与此习惯用法匹配的内容(而不是整个“搜索”字符串):
…因此,为了完整地捕获搜索的字符串,我们应该再次将其用括号括起来:
$ perl -e '$str="the variable Xy = 3 in this case"; print $str =~ /(Xy = (\d))/ ; \
print "\n";'
Xy = 33
…但是,我们没有得到我们期望的结果,因为现在有两个匹配项,$1
和$2
;显然,成语“print$str=~/…/
”输出所有匹配项(在本例中,$1$2
”)
因此,为了在这个嵌套匹配的情况下只获取搜索的字符串,我们现在必须显式地只指定$1
:
$ perl -e '$str="the variable Xy = 3 in this case"; $str =~ /(Xy = (\d))/ ; \
print "$1 \n";'
Xy = 3
编辑2013年10月:Via-也可以针对一个班轮:
$ perl -e '$str="the variable Xy = 3 in this case"; \
print ( ( $str =~ /(Xy = (\d))/ )[1] ); print "\n";'
3
…但是,请注意,print
需要第二组括号,以便直接使用regex返回的(匿名)数组
最后,在多行上下文中,确保首先将整个文件/文本“slurp”为单个字符串;然后使用/s
(单行模式=换行匹配)和/g
(全局/多个匹配)-最后,确保匹配表达式处于while
循环中,以便迭代所有匹配:
$ echo "some data
IN
123
OUT
some more data
is about to follow:
IN
256
OUT
done with data
out" | perl -e '$str = do { local $/; <> }; while ($str =~ /(IN(.*?)OUT)/sg) { print "$1\n"} '
IN
123
OUT
IN
256
OUT
$echo”一些数据
在里面
123
出来
更多数据
即将进行的工作如下:
在里面
256
出来
用数据完成
out“|perl-e'$str=do{local$/;};而($str=~/(IN(.*out)/sg){print“$1\n”}”
在里面
123
出来
在里面
256
出来
好吧,希望这对某人有所帮助,干杯!接受答案的命令行版本非常紧凑:
perl -ne '/Total Successful Transactions = (\d+)/ && print "$1\n";'
谢谢,但是可以说打印字符串中出现“成功传输”的值(数字)吗?应该检查匹配是否失败(将$number设置为undef)。另外,您可以说“在最后一次“但不要实现这一点。谢谢。”谢谢你让我知道。这是一次失误。我已经编辑了我的答案。对于大的输入,对整个文件进行slurp可能会很昂贵。另一种方法是使用范围运算符:
perl-ne'print if/IN/./OUT/'
$ perl -e '$str="the variable Xy = 3 in this case"; print $str =~ /Xy = (\d)/ ; \
print "\n";'
3
$ perl -e '$str="the variable Xy = 3 in this case"; print $str =~ /(Xy = (\d))/ ; \
print "\n";'
Xy = 33
$ perl -e '$str="the variable Xy = 3 in this case"; $str =~ /(Xy = (\d))/ ; \
print "$1 \n";'
Xy = 3
$ perl -e '$str="the variable Xy = 3 in this case"; \
print ( ( $str =~ /(Xy = (\d))/ )[1] ); print "\n";'
3
$ echo "some data
IN
123
OUT
some more data
is about to follow:
IN
256
OUT
done with data
out" | perl -e '$str = do { local $/; <> }; while ($str =~ /(IN(.*?)OUT)/sg) { print "$1\n"} '
IN
123
OUT
IN
256
OUT
perl -ne '/Total Successful Transactions = (\d+)/ && print "$1\n";'