2010-09-08 67 views
1
/^([a-z]:)?\//i 

我不太明白这个正则表达式的?如果我不得不从我的理解解释:这个正则表达式在做什么?

比赛开始“组别1是A到Z和”外?(这我不吨得到它做的)\/,这使得它匹配/和选项/i“不区分大小写”。

我意识到,这将返回0或1静不肯定的,为什么,因为?

这是相匹配的目录路径或东西吗?

如果我测试:

$var = 'test'会得到0,而$var ='/test';将获得1但$var = 'test/'得0

所以任何以/开始将获得1和其他任何0

可有人用我的基本术语解释这个正则表达式?

回答

7

?将匹配前面的模式中的一个或不匹配。

?  Match 1 or 0 times 

参见:perldoc perlre

说明:

/.../i # case insensitive 

^(...) # match at the beginning of the string 

[a-z]: # one character between 'a' and 'z' followed by a colon 

(...)? # zero or one time of the group, enclosed in() 

所以在英语匹配任何条件,其与/(斜杠)开头或某个字母后跟一个冒号后面是/。这看起来像它匹配unix和windows之间的路径名,例如 它将匹配:

/home/user 

C:/Applications 

+0

再次感谢您对现在清楚的长期解释。 – Prix 2010-09-08 13:12:08

+0

you''re'欢迎 - – miku 2010-09-08 13:28:06

8

它一个小写或大写字母([a-z]i改性剂)匹配定位在输入字符串的开始(^)后跟冒号(:)全部可选地(?),接着是正斜杠\/

简而言之:

^   # match the beginning of the input 
(   # start capture group 1 
    [a-z] # match any character from the set {'A'..'Z', 'a'..'z'} (with the i-modifier!) 
    :  # match the character ':' 
)?   # end capture group 1 and match it once or none at all 
\/   # match the character '/' 
+0

非常详细的解释! – jwueller 2010-09-08 13:03:53

2

它看起来就像是在寻找一个 “扎根” 的路径。它将成功地匹配任何以正斜杠(/ test)开头的字符串或驱动器号后跟冒号的字符串,后跟正斜杠(c:/ test)。

+1

而且可能很糟糕,因为如果你的路径以一个字母和一个冒号开头,你可能会反斜杠而不是斜杠。 – geoffspear 2010-09-08 13:04:20

+0

感谢这解释了我错过了它是为win和linux而创建的。 – Prix 2010-09-08 13:09:22

1

具体来说,问号使得某些东西是可选的。它适用于括号内的部分,后面是一个字母后跟冒号。

的事情,将匹配:(?上面最后一个项目就是为什么有)

C:/ 
a:/ 
/

的东西会不匹配:

C: 
a: 
ab:/ 
a/ 
9

YAPE::Regex::Explain

#!/usr/bin/perl 

use strict; use warnings; 

use YAPE::Regex::Explain; 
print YAPE::Regex::Explain->new(qr/^([a-z]:)?\//i)->explain; 
The regular expression: 

(?i-msx:^([a-z]:)?/) 

matches as follows: 

NODE      EXPLANATION 
---------------------------------------------------------------------- 
(?i-msx:     group, but do not capture (case-insensitive) 
         (with^and $ matching normally) (with . not 
         matching \n) (matching whitespace and # 
         normally): 
---------------------------------------------------------------------- 
^      the beginning of the string 
---------------------------------------------------------------------- 
    (      group and capture to \1 (optional 
          (matching the most amount possible)): 
---------------------------------------------------------------------- 
    [a-z]     any character of: 'a' to 'z' 
---------------------------------------------------------------------- 
    :      ':' 
---------------------------------------------------------------------- 
)?      end of \1 (NOTE: because you're using a 
          quantifier on this capture, only the LAST 
          repetition of the captured pattern will be 
          stored in \1) 
---------------------------------------------------------------------- 
/      '/' 
---------------------------------------------------------------------- 
)      end of grouping 
----------------------------------------------------------------------
+0

使用正则表达式来解码正则表达式...这需要一些习惯。 – Zaid 2010-09-08 13:50:56

相关问题