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

来源:互联网 发布:打印排版软件有哪些 编辑:程序博客网 时间:2024/06/05 21:23

#! /usr/bin/perl

#完整版,综合代码介绍1,2


undef $/; # Enter "file-slurp" mode
$text = <>; # Slurp up the first file given on the command line.
$text =~ s/&/&amp;/g; # Make the basic HTML . . .
$text =~ s/</&lt;/g; # . . . characters &, <, and > . . .
$text =~ s/>/&gt;/g; # . . . HTML safe.
$text =~ s/^\s+$/<p>/mg; # Separate paragraphs.


$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
}{<a href="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;


print $text;

原创粉丝点击