⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pattern matching.mht

📁 Perl 编程技巧大全。适合初学者阅读。
💻 MHT
📖 第 1 页 / 共 5 页
字号:
color=3D#00cd00>"Failed!\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe># Last fish is blue.
#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN303>Matching Multiple Lines</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
# <FONT size=3D-1><A =
href=3D"http://pleac.sourceforge.net/include/perl/ch06/killtags">download=
 the following standalone program</A></FONT>
#!/usr/bin/perl
# killtags - very bad html tag killer
</FONT><FONT color=3D#ff7f50>undef</FONT> $/<FONT =
color=3D#00ffff>;</FONT>           <FONT color=3D#bebebe># each read is =
whole file
</FONT><FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT>&lt;&gt;<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>        <FONT color=3D#bebebe># get one whole =
file at a time
</FONT>    <FONT color=3D#ff7f50>s</FONT>/&lt;.*?&gt;//gs<FONT =
color=3D#00ffff>;</FONT>    <FONT color=3D#bebebe># strip tags =
(terribly)
</FONT>    <FONT color=3D#ff7f50>print</FONT><FONT =
color=3D#00ffff>;</FONT>          <FONT color=3D#bebebe># print file to =
STDOUT
</FONT><FONT color=3D#00ffff>}</FONT>

<FONT color=3D#bebebe>#-----------------------------
# <FONT size=3D-1><A =
href=3D"http://pleac.sourceforge.net/include/perl/ch06/headerfy">download=
 the following standalone program</A></FONT>
#!/usr/bin/perl
# headerfy: change certain chapter headers to html
</FONT>$/ =3D <FONT color=3D#00cd00>''</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>while</FONT> <FONT color=3D#00ffff>(</FONT> =
&lt;&gt; <FONT color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>{</FONT>   =
           <FONT color=3D#bebebe># fetch a paragraph
</FONT>    <FONT color=3D#ff7f50>s</FONT><FONT color=3D#00ffff>{</FONT>
        \A                  <FONT color=3D#bebebe># start of record
</FONT>        <FONT color=3D#00ffff>(</FONT>                   <FONT =
color=3D#bebebe># capture in $1
</FONT>            Chapter         <FONT color=3D#bebebe># text string
</FONT>            \s+             <FONT color=3D#bebebe># mandatory =
whitespace
</FONT>            \d+             <FONT color=3D#bebebe># decimal =
number
</FONT>            \s*             <FONT color=3D#bebebe># optional =
whitespace
</FONT>            :               <FONT color=3D#bebebe># a real colon
</FONT>            . *             <FONT color=3D#bebebe># anything not =
a newline till end of line
</FONT>        <FONT color=3D#00ffff>)</FONT>
    <FONT color=3D#00ffff>}{</FONT>&lt;H1&gt;$<FONT =
color=3D#cdcd00>1</FONT>&lt;/H1&gt;<FONT color=3D#00ffff>}</FONT>gx<FONT =
color=3D#00ffff>;</FONT>
    <FONT color=3D#ff7f50>print</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>

<FONT color=3D#bebebe>#-----------------------------
#% perl -00pe 's{\A(Chapter\s+\d+\s*:.*)}{&lt;H1&gt;$1&lt;/H1&gt;}gx' =
datafile
#-----------------------------
</FONT>$/ =3D <FONT color=3D#00cd00>''</FONT><FONT =
color=3D#00ffff>;</FONT>            <FONT color=3D#bebebe># paragraph =
read mode for readline access
</FONT><FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT>&lt;ARGV&gt;<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#ff7f50>m</FONT><FONT =
color=3D#bebebe>#^START(.*?)^END#sm) {  # /s makes . span line =
boundaries
</FONT>                                    <FONT color=3D#bebebe># /m =
makes ^ match near newlines
</FONT>        <FONT color=3D#ff7f50>print</FONT> <FONT =
color=3D#00cd00>"chunk $. in $ARGV has &lt;&lt;$1&gt;&gt;\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
    <FONT color=3D#00ffff>}</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN306>Reading Records with a Pattern =
Separator</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ff7f50>undef</FONT> $/<FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#cdad00>@chunks</FONT> =3D <FONT =
color=3D#ff7f50>split</FONT><FONT color=3D#00ffff>(</FONT>/pattern/, =
&lt;FILEHANDLE&gt;<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
# .Ch, .Se and .Ss divide chunks of STDIN
</FONT><FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>local</FONT> $/ =3D <FONT =
color=3D#ff7f50>undef</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#cdad00>@chunks</FONT> =3D <FONT =
color=3D#ff7f50>split</FONT><FONT color=3D#00ffff>(</FONT>/^\.<FONT =
color=3D#00ffff>(</FONT>Ch|Se|Ss<FONT color=3D#00ffff>)</FONT>$/<FONT =
color=3D#ff7f50>m</FONT>, &lt;&gt;<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"I read =
"</FONT>, <FONT color=3D#ff7f50>scalar</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#cdad00>@chunks</FONT><FONT =
color=3D#00ffff>)</FONT>, <FONT color=3D#00cd00>" chunks.\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN309>Extracting a Range of Lines</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT>&lt;&gt;<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>if</FONT> <FONT color=3D#00ffff>(</FONT>/<FONT =
color=3D#ffa500>BEGIN</FONT> PATTERN/ .. /<FONT =
color=3D#ffa500>END</FONT> PATTERN/<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
        <FONT color=3D#bebebe># line falls between BEGIN and END in the
</FONT>        <FONT color=3D#bebebe># text, inclusive.
</FONT>    <FONT color=3D#00ffff>}</FONT>
<FONT color=3D#00ffff>}</FONT>

<FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT>&lt;&gt;<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#00ffff>(</FONT>$FIRST_LINE_NUM .. $LAST_LINE_NUM<FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>{</FONT>
        <FONT color=3D#bebebe># operate only between first and last =
line, inclusive.
</FONT>    <FONT color=3D#00ffff>}</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT>&lt;&gt;<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>if</FONT> <FONT color=3D#00ffff>(</FONT>/<FONT =
color=3D#ffa500>BEGIN</FONT> PATTERN/ ... /<FONT =
color=3D#ffa500>END</FONT> PATTERN/<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
        <FONT color=3D#bebebe># line is between BEGIN and END on =
different lines
</FONT>    <FONT color=3D#00ffff>}</FONT>
<FONT color=3D#00ffff>}</FONT>

<FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT>&lt;&gt;<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#00ffff>(</FONT>$FIRST_LINE_NUM ... $LAST_LINE_NUM<FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>{</FONT>
        <FONT color=3D#bebebe># operate only between first and last =
line, but not same
</FONT>    <FONT color=3D#00ffff>}</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
# command-line to print lines 15 through 17 inclusive (see below)
</FONT>perl -<FONT color=3D#98fb98>ne</FONT> <FONT =
color=3D#00cd00>'print if 15 .. 17'</FONT> datafile

<FONT color=3D#bebebe># print out all &lt;XMP&gt; .. &lt;/XMP&gt; =
displays from HTML doc
</FONT><FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT>&lt;&gt;<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#ff7f50>print</FONT> <FONT color=3D#ffa500>if</FONT> =
<FONT color=3D#ff7f50>m</FONT><FONT color=3D#bebebe>#&lt;XMP&gt;#i .. =
m#&lt;/XMP&gt;#i;
</FONT><FONT color=3D#00ffff>}</FONT>
<FONT color=3D#43cd80>    </FONT>
<FONT color=3D#bebebe># same, but as shell command
# perl -ne 'print if m#&lt;XMP&gt;#i .. m#&lt;/XMP&gt;#i' document.html
#-----------------------------
# perl -ne 'BEGIN { $top=3D3; $bottom=3D5 }  print if $top .. $bottom' =
/etc/passwd        # previous command FAILS
# perl -ne 'BEGIN { $top=3D3; $bottom=3D5 } \
#     print if $. =3D=3D $top .. $. =3D=3D     $bottom' /etc/passwd    # =
works
# perl -ne 'print if 3 .. 5' /etc/passwd   # also works
#-----------------------------
</FONT><FONT color=3D#ff7f50>print</FONT> <FONT =
color=3D#ffa500>if</FONT> /begin/ .. /end/<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#ffa500>if</FONT> =
/begin/ ... /end/<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT>&lt;&gt;<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
    $in_header =3D   <FONT color=3D#cdcd00>1</FONT>  .. /^$/<FONT =
color=3D#00ffff>;</FONT>
    $in_body   =3D /^$/ .. <FONT color=3D#98fb98>eof</FONT><FONT =
color=3D#00ffff>();</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#cdcd00>%seen</FONT> =3D <FONT =
color=3D#00ffff>();</FONT>
<FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT>&lt;&gt;<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>next</FONT> <FONT =
color=3D#ffa500>unless</FONT> /^From:?\s/i .. /^$/<FONT =
color=3D#00ffff>;</FONT>
    <FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT>/<FONT color=3D#00ffff>([</FONT>^&lt;&gt;<FONT =
color=3D#00ffff>()</FONT>,<FONT color=3D#00ffff>;</FONT>\s<FONT =
color=3D#00ffff>]</FONT>+\@<FONT color=3D#00ffff>[</FONT>^&lt;&gt;<FONT =
color=3D#00ffff>()</FONT>,<FONT color=3D#00ffff>;</FONT>\s<FONT =
color=3D#00ffff>]</FONT>+<FONT color=3D#00ffff>)</FONT>/g<FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>{</FONT>
        <FONT color=3D#ff7f50>print</FONT> <FONT =
color=3D#00cd00>"$1\n"</FONT> <FONT color=3D#ffa500>unless</FONT> <FONT =
color=3D#cdcd00>$seen</FONT><FONT color=3D#00ffff>{</FONT>$<FONT =
color=3D#cdcd00>1</FONT><FONT color=3D#00ffff>}</FONT>++<FONT =
color=3D#00ffff>;</FONT>
    <FONT color=3D#00ffff>}</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN312>Matching Shell Globs as Regular=20
Expressions</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>sub</FONT> <FONT =
color=3D#b2dfee>glob2pat</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>my</FONT> <FONT =
color=3D#9ac0cd>$globstr</FONT> =3D <FONT =
color=3D#ff7f50>shift</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#ffa500>my</FONT> <FONT color=3D#cdcd00>%patmap</FONT> =
=3D <FONT color=3D#00ffff>(</FONT>
	 <FONT color=3D#00cd00>'*'</FONT> =3D&gt; <FONT =
color=3D#00cd00>'.*'</FONT>,
	 <FONT color=3D#00cd00>'?'</FONT> =3D&gt; <FONT =
color=3D#00cd00>'.'</FONT>,
	 <FONT color=3D#00cd00>'['</FONT> =3D&gt; <FONT =
color=3D#00cd00>'['</FONT>,
	 <FONT color=3D#00cd00>']'</FONT> =3D&gt; <FONT =
color=3D#00cd00>']'</FONT>,
    <FONT color=3D#00ffff>);</FONT>
    $globstr =3D~ <FONT color=3D#ff7f50>s</FONT><FONT =
color=3D#00ffff>{(</FONT>.<FONT color=3D#00ffff>)}</FONT> <FONT =
color=3D#00ffff>{</FONT> <FONT color=3D#cdcd00>$patmap</FONT><FONT =
color=3D#00ffff>{</FONT>$<FONT color=3D#cdcd00>1</FONT><FONT =
color=3D#00ffff>}</FONT> || <FONT color=3D#00cd00>"\Q$1"</FONT> <FONT =
color=3D#00ffff>}</FONT><FONT color=3D#98fb98>ge</FONT><FONT =
color=3D#00ffff>;</FONT>
    <FONT color=3D#ffa500>return</FONT> <FONT color=3D#00cd00>'^'</FONT> =
. $globstr . <FONT color=3D#00cd00>'$'; #'</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -