/^([a-z]:)?\//i
Run Code Online (Sandbox Code Playgroud)
?如果我必须从我理解的内容中解释它,我不太明白这个正则表达式是什么:
匹配开始"Group1是a到z和:"外部?(我不知道它做什么)\/使得它匹配/并且选项/i"不区分大小写".
我意识到这将返回0或1不安静确定为什么因为 ?
这是匹配目录路径还是什么?
如果我测试它:
$var = 'test'得到0而$var ='/test';得到1但$var = 'test/'得到0
所以任何开头/都会得到1和其他任何东西0.
有人可以用基本的术语向我解释这个正则表达式吗?
#!/usr/bin/perl
use strict; use warnings;
use YAPE::Regex::Explain;
print YAPE::Regex::Explain->new(qr/^([a-z]:)?\//i)->explain;
Run Code Online (Sandbox Code Playgroud)
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
----------------------------------------------------------------------
它匹配一个小写或大写字母([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 '/'
Run Code Online (Sandbox Code Playgroud)
? 将匹配前一个模式中的一个或不匹配.
? Match 1 or 0 times
Run Code Online (Sandbox Code Playgroud)
说明:
/.../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 ()
Run Code Online (Sandbox Code Playgroud)
所以在英语中:匹配任何以/(斜杠)或某个字母开头,后跟冒号后跟a的东西/.这看起来像是在unix和windows中匹配路径名,例如它匹配:
/home/user
Run Code Online (Sandbox Code Playgroud)
和
C:/Applications
Run Code Online (Sandbox Code Playgroud)
等等