我有这个用于从文本中提取URL的小Perl脚本:
#!/usr/bin/perl
while ( <STDIN> )
{
if ( /(http|ftp|https):\/\/([\w\-_]+(?:(?:\.[\w\-_]+)+))([\w\-\.,@?^=%&./~\+#]*[\w\-\@?^=%&/~\+#])?/ )
{
print;
}
}
Run Code Online (Sandbox Code Playgroud)
当我在Textwrangler中搜索时,正则表达式工作正常,但是当我通过此脚本运行它时,我收到以下错误:
$ cat file.txt | perl myscript.pl
Unmatched [ in regex; marked by <-- HERE in m/(http|ftp|https)://([\w\-_]+(?:(?:\.[\w\-_]+)+))([ <-- HERE \w\-\.,@?^=%&./ at myscript.pl line 5.
Run Code Online (Sandbox Code Playgroud)
小智 5
最后/的角色类也需要进行转义.您可以考虑使用/x修饰符使其更具可读性,并使用不同的分隔符来避免"倾斜的牙签".此外,\w该类已包含下划线,并且.字符类内部始终按字面顺序匹配.
if (m{
(http|ftp|https)://
( [\w\-]+ (?: (?:\.[\w\-]+)+ ) )
(
[\w\-.,@?^=%&/~+#]*
[\w\-@?^=%&/~+#]
)?
}x
) {
print;
}
Run Code Online (Sandbox Code Playgroud)
通过这样做,也很清楚最后两个字符类是不是真正的字符类:
这:/[&]*/
肯定会匹配&,而且a&m&p,ma,pa,&;,等等,等等.