此代码段摘自《精通正则表达式》一书。我很难理解带有负向后看的最后一部分(注释# Not allowed to end with [.,?!])。该表达式如何从[?!,.]http://www.google.com/foo!中删除http://www.google.com/bar\!

# Turn HTTP URLs into links . . .
$text =~ s{
   \b
   # Capture the URL to $1 . . .
   (
      http:// [-a-z0-9]+(\.[-a-z0-9]+)*\.(com|edu|info) \b   # hostname
      (
         / [-a-z0-9_:\@&?=+,.!/~*'%\$]* # Optional path
         (?<![.,?!])    # Path not allowed to end with [.,?!]
      )?
   )
}{<a href="$1">$1</a>}gix;

print $text; # Finally, display the HTML-ized text.

最佳答案

它不会。它在表达式的一部分中匹配了可选路径,并且由于http://www.google.com中没有路径,因此它将无效。

07-24 18:36
查看更多