Mastering Regular Expression 代码介绍 2 - HTML 格式转换

来源:互联网 发布:淘宝助理属性不一致 编辑:程序博客网 时间:2024/05/16 15:17

#HTML 格式转换,使用regular library 简化程序


#Building a regex library

$HostnameRegex = qr/[-a-z0-9]+(\.[-a-z0-9]+)*\.(com|edu|info)/i;

 

# Turn email addresses into links . . .

$text =~ s{

   \b

   # Capture the address to $1 . . .

   (

    \w[-.\w]*                         # username

     \@

     $HostnameRegex  # hostname

   )

   \b

}{<ahref="mailto:$1">$1</a>}gix;

 

# Turn HTTP URLs into links . . .

$text =~ s{

   \b

   # Capture the URL to $1 . . .

   (

     http:// $HostnameRegex \b         # hostname

      (

        / [-a-z0-9_:\@&?=+,.!/~*'%\$]* # Optional path

          (?<![.,?!])    # not allowed to end with [.,?!]

     )?

   )

}{<a href="$1">$1</a>}gix;
原创粉丝点击