2012-09-10 43 views
0

我找到一個Perl(實際上PDL)的程序如下聲明:神祕的Perl表達式

/\/([\w]+)$/i; 

有人能解讀這些對我來說,在Perl編程的徒弟?

回答

10

當然,我會從裏面解釋一下:

\w - 匹配可在單詞中使用的single character(字母數字,加'_')

[...] - 匹配中的單個字符

[\w] - 相匹配,可以在一個字(有點多餘這裏)

+使用單個字符 - 前一個字符,repeating as many times儘可能匹配,但必須至少出現一次。

[\w]+ - 匹配一組單詞字符,多次結束。這會找到一個詞。

(...) - grouping。以後記住這組字符。

([\w]+) - 匹配一個單詞,並記住它供以後

$ - end-of-line。在一行

([\w]+)$結束匹配的東西 - 一行匹配的最後一個單詞,並記住它供以後

\/ - 單斜線字符「/」。它必須被反斜槓轉義,因爲斜線是特殊的。

\/([\w]+)$ - 匹配一行中的最後一個單詞,在斜線'/'後面,並且稍後記住單詞。這可能從路徑中抓取目錄/文件名。

/.../ - match語法

/.../i - i表示case-insensitive

組合的設計:

/\/([\w]+)$/i; - 一行匹配的最後一個字,並記住它供以後;這個詞必須在斜線後出現。基本上,從絕對路徑獲取文件名。不區分大小寫的部分是不相關的,\w將已經匹配這兩種情況。

更多關於Perl的正則表達式這裏的細節:http://www.troubleshooters.com/codecorn/littperl/perlreg.htm

而作爲JRFerguson指出,YAPE::Regex::Explain是標化正則表達式,並解釋了件非常有用。

+0

謝謝蒂姆......出色的描述,完全符合代碼的上下文。看起來我需要更多的正則表達式經歷40多年的編程! – user65837

2
/\/([\w]+)$/i; 

這是一個正則表達式,如果它是一個完整的語句時,它被應用到$_變量,像這樣:由字母和數字組成

$_ =~ /\/([\w]+)$/i; 

它尋找一個斜線\/,其次\w+,接着是行$的結束。它還捕獲()字母數字字符串,該字符串以變量$1結尾。最後的/i使其不區分大小寫,在這種情況下不起作用。

0

這在$1比較$_以一個斜線跟着一個或多個字符(不區分大小寫),並將其存儲

$_ value  then  $1 value 
------------------------------ 
"/abcdes"  |  "abcdes" 
"foo/bar2" |  "bar2" 
"foobar"  |  undef  # no slash so doesn't match 
+0

'$ 1'是undef,除非以前的匹配,在這種情況下它的值不變。 – Toto

5

你會發現Yape::Regex::Explain模塊值得安裝。

#!/usr/bin/env perl 
use YAPE::Regex::Explain; 
#...may need to single quote $ARGV[0] for the shell... 
print YAPE::Regex::Explain->new($ARGV[0])->explain; 

假設這個腳本被命名爲 'rexplain' 做:

$ ./rexplain '/\/([\w]+)$/i' 

...獲得:

The regular expression: 

(?-imsx:/\/([\w]+)$/i) 

matches as follows: 

NODE      EXPLANATION 
---------------------------------------------------------------------- 
(?-imsx:     group, but do not capture (case-sensitive) 
         (with^and $ matching normally) (with . not 
         matching \n) (matching whitespace and # 
         normally): 
---------------------------------------------------------------------- 
/      '/' 
---------------------------------------------------------------------- 
    \/      '/' 
---------------------------------------------------------------------- 
    (      group and capture to \1: 
---------------------------------------------------------------------- 
    [\w]+     any character of: word characters (a-z, 
          A-Z, 0-9, _) (1 or more times (matching 
          the most amount possible)) 
---------------------------------------------------------------------- 
         (with^and $ matching normally) (with . not 
         matching \n) (matching whitespace and # 
         normally): 
---------------------------------------------------------------------- 
/      '/' 
---------------------------------------------------------------------- 
    \/      '/' 
---------------------------------------------------------------------- 
    (      group and capture to \1: 
---------------------------------------------------------------------- 
    [\w]+     any character of: word characters (a-z, 
          A-Z, 0-9, _) (1 or more times (matching 
          the most amount possible)) 
---------------------------------------------------------------------- 
)      end of \1 
---------------------------------------------------------------------- 
    $      before an optional \n, and the end of the 
          string 
---------------------------------------------------------------------- 
    /i      '/i' 
---------------------------------------------------------------------- 
)      end of grouping 
---------------------------------------------------------------------- 

UPDATE:

參見:https://stackoverflow.com/a/12359682/1015385。如前所述有與模塊的文檔中:

沒有爲Perl的5.6版本,在5.10特別添加任何 結構後添加正則表達式語法的支持。

+0

哦,這很酷。感謝那。 – Tim

0

Online Regex Analyzer值得一提。這裏有一個link來解釋你的正則表達式的含義,並粘貼在這裏記錄。

序列:匹配所有的以下的以

/             (slash) 
               --+ 
Repeat           | (in GroupNumber:1) 
    AnyCharIn[ WordCharacter] one or more times | 
               --+ 
EndOfLine 
2

雖然它並不能幫助「解釋」正則表達式,一旦你有一個測試用例,達米安的新Regexp::Debugger是一個很酷的工具來實際看什麼在匹配期間發生。安裝它,然後在命令行上執行rxrx以啓動調試器,然後鍵入/\/([\w]+)$/'/r'(例如),最後輸入m開始匹配。然後,您可以通過反覆按Enter鍵來遍歷調試器。真酷!

+0

+1的確,很酷! – JRFerguson