2011-04-11 53 views
21

什麼是perl正則表達式來打印匹配的值?例如,我想從此字符串打印195Perl正則表達式 - 打印匹配值

"2011-04-11 00:39:28,736::[main]::INFO (Main.java:73) Test.Main::main() Total Successful Transactions = 195". 

我該怎麼做?在此先感謝

回答

28

您可以在正則表達式中使用圓括號來捕獲子字符串。捕獲的組存儲在$ 1,$ 2等等中。例如:

while (<>) { 
    if (/Total Successful Transactions = (\d+)/) { 
     print "$1\n"; 
    } 
} 

,或者稍短:

while (<>) { 
    print "$1\n" if /Total Successful Transactions = (\d+)/; 
} 
0

像這樣:

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。

+0

謝謝你,但是否可以說打印的值(數),其中「成功Tranaction」是目前的字符串中。 – Allwyn 2011-04-11 06:10:46

+0

嗨馬克斯克斯給出了答案。 – Allwyn 2011-04-11 06:12:42

1
my $str = '"2011-04-11 00:39:28,736::[main]::INFO (Main.java:73) Test.Main::main() Total Successful Transactions = 195".'; 

$str =~ /Total Successful Transactions = (\d+)/; 
print $1; 

195只會".之前存儲在$ 1中

2

檢查在最後的數字。

#!/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; 
+0

+1僅用於返回值。 – daxim 2011-04-11 11:00:08

+0

應檢查匹配是否失敗(將$ number設置爲undef)。另外,你說「在最後」,但不要實現。 – ysth 2011-04-11 16:55:20

+0

_ [daxim](http://stackoverflow.com/users/46395/daxim)_:謝謝。 _ [ysth](http://stackoverflow.com/users/17389/ysth)_:謝謝你讓我知道。這是一個小姐。我編輯了我的答案。 – 2011-04-11 18:10:46

7

如何只:

print $string =~ /Total Successful Transactions = (\d+)/; 

你很少真正需要使用$1和朋友。

+0

+1僅用於返回值。 – daxim 2011-04-11 11:00:30

4
/(\d+$)/; 
print $1; 

我想你只是想要號碼195。對?

11

只是想重新構造一下前面的答案;注意在這種情況下:

$ perl -e '$str="the variable Xy = 3 in this case"; print $str =~ /Xy = 3/;' 
1 

......我們得到1被打印出來 - 如「是的,我得到了一個匹配」。如果我們想返回匹配的文本部分,作爲@markusk指出,「在正則表達式使用括號來捕獲子」:但是

$ perl -e '$str="the variable Xy = 3 in this case"; print $str =~ /(Xy = 3)/;' 
Xy = 3 

,請注意,你可能有連接字符串用這個成語,當問題捕獲:

$ 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";' 
3 

...因此,捕捉到的全部搜索到的字符串,我們應該在括號中再次把它包:

$ 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月:通過Assign one of multiple regex matches to variable as Perl one-liner (dereference array?) - 也這可以爲一個完成眼線:

$ perl -e '$str="the variable Xy = 3 in this case"; \ 
print (($str =~ /(Xy = (\d))/)[1]); print "\n";' 
3 

...但是,請注意需要一個第二包圍組括號爲print直接與正則表達式返回工作崗位(anonymo我們?)陣列。

 

最後,在一個多背景下,首先要確保「啜食」的整個文件/文成一個字符串;然後使用/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 

嗯,希望這可以幫助別人,
乾杯!

+0

Sl the整個文件對於大型輸入可能是昂貴的。另一種方法是使用範圍運算符:'perl -ne'print if/IN /../ OUT /'' – markusk 2018-01-11 12:20:29

1

通過@markusk命令行版本接受的答案是相當緊湊:

perl -ne '/Total Successful Transactions = (\d+)/ && print "$1\n";'