据我所知,我真的不知道为什么它会在“测试”处停止,它会用任何内容替换字符串开头的任何字符,那么为什么它会在测试处停止?
因为是\/
模式的一部分。
# V here
$url =~ s/^.*\///;
如果代码使用不同的引号分隔符会更清楚,这在 Perl 中是可能的。这样,这里就不会有倾斜牙签综合症。
$url =~ s{^.*/}{};
请注意,默认情况下它是贪婪的,所以它会吞噬所有的斜线,直到最后一个。
您可以在调试模式下使用re
pragma来了解更多关于正则表达式引擎在后台执行的操作。
use re 'debug';
my $url = "http://perltest.my-mobile.org/c/test.cgi?u=USER&p=PASS";
$url =~ s{^.*/}{};
这将输出到 STDERR。
Compiling REx "^.*/"
Final program:
1: SBOL /^/ (2)
2: STAR (4)
3: REG_ANY (0)
4: EXACT </> (6)
6: END (0)
floating "/" at 0..9223372036854775807 (checking floating) anchored(SBOL) minlen 1
Matching REx "^.*/" against "http://perltest.my-mobile.org/c/test.cgi?u=USER&p=PASS"
Intuit: trying to determine minimum start position...
doing 'check' fbm scan, [0..54] gave 5
Found floating substr "/" at offset 5 (rx_origin now 0)...
(multiline anchor test skipped)
Intuit: Successfully guessed: match at offset 0
0 <> <http://per> | 0| 1:SBOL /^/(2)
0 <> <http://per> | 0| 2:STAR(4)
| 0| REG_ANY can match 54 times out of 2147483647...
31 <org/c> </test.cgi?> | 1| 4:EXACT </>(6)
32 <rg/c/> <test.cgi?u> | 1| 6:END(0)
Match successful!
Freeing REx: "^.*/"