Regex 试图理解这个用perl正则表达式括起来的字符类?
下面是我正在玩的脚本。使用下面的脚本,它将打印Regex 试图理解这个用perl正则表达式括起来的字符类?,regex,perl,pattern-matching,character-class,Regex,Perl,Pattern Matching,Character Class,下面是我正在玩的脚本。使用下面的脚本,它将打印a $tmp = "cd abc/test/."; if ( $tmp =~ /cd ([\w\/\.])/ ) { print $1."\n"; } 但如果我将其更改为: $tmp = "cd abc/test/."; if ( $tmp =~ /cd ([\w\/\.]+)/ ) { print $1."\n"; } 然后打印:cd abc/test/ 根据我的理解+匹配一个或多个匹配序列,如果我错了,请纠正我。但是为什么在第一种
a
$tmp = "cd abc/test/.";
if ( $tmp =~ /cd ([\w\/\.])/ ) {
print $1."\n";
}
但如果我将其更改为:
$tmp = "cd abc/test/.";
if ( $tmp =~ /cd ([\w\/\.]+)/ ) {
print $1."\n";
}
然后打印:cd abc/test/
根据我的理解+匹配一个或多个匹配序列,如果我错了,请纠正我。但是为什么在第一种情况下它只匹配a
?我认为它应该什么都不匹配
谢谢。在正则表达式中,括号中的字符仅适用于给定括号内一个字符的匹配。换句话说,
[\w\/\.]
正好匹配以下字符之一:
“\ucode>(\w
)
\.
——再次转义,因为
表示除换行符以外的任何字符)李>
由于/cd([\w\/\.]./
只将一个字符捕获到$1
,因此它会捕获第一个字符,在本例中,第一个字符是“a”
您是正确的,+
允许匹配一个或多个这样的字符。由于正则表达式在默认情况下非常匹配,因此在第二次匹配中,您应该获得$1
的所有“abc/test/”
如果您还没有这样做,您可能需要仔细阅读。您是对的。在第一种情况下,您匹配该字符类中的单个字符,而在第二种情况下,您至少匹配一个字符,并在第一个字符之后匹配尽可能多的字符 第一个:
"
cd\ # Match the characters “cd ” literally
( # Match the regular expression below and capture its match into backreference number 1
[\w\/\.] # Match a single character present in the list below
# A word character (letters, digits, etc.)
# A / character
# A . character
)
"
"
cd\ # Match the characters “cd ” literally
( # Match the regular expression below and capture its match into backreference number 1
[\w\/\.] # Match a single character present in the list below
# A word character (letters, digits, etc.)
# A / character
# A . character
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
"
第二个:
"
cd\ # Match the characters “cd ” literally
( # Match the regular expression below and capture its match into backreference number 1
[\w\/\.] # Match a single character present in the list below
# A word character (letters, digits, etc.)
# A / character
# A . character
)
"
"
cd\ # Match the characters “cd ” literally
( # Match the regular expression below and capture its match into backreference number 1
[\w\/\.] # Match a single character present in the list below
# A word character (letters, digits, etc.)
# A / character
# A . character
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
"
它不应该在开头打印“cd”。@rashid当您需要关于regexp的解释时,可以使用module。