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

📄 perl6.htm

📁 Perl作为一门重要的工程语言
💻 HTM
📖 第 1 页 / 共 2 页
字号:
                <td valign=top width=147>[ \r\t\n\f]</td>
              </tr>
              <tr>
                <td valign=top width=135><tt>\S</tt></td>
                <td valign=top width=228>非空白</td>
                <td valign=top width=147><tt>[^ \r\t\n\f]</tt></td>
              </tr>
            </table>
            <p>&nbsp;&nbsp; 例:/[\da-z]/匹配任意数字或小写字母。<br>
              <a name=9、匹配任意字符>9、匹配任意字符</a><br>
              &nbsp;&nbsp; 字符&quot;.&quot;匹配除换行外的所有字符,通常与*合用。<br>
              <a name=10、匹配指定数目的字符>10、匹配指定数目的字符</a><br>
              &nbsp;&nbsp; 字符对{}指定所匹配字符的出现次数。如:/de{1,3}f/匹配def,deef和deeef;/de{3}f/匹配deeef;/de{3,}f/匹配不少于3个e在d和f之间;/de{0,3}f/匹配不多于3个e在d和f之间。<br>
              <a name=11、指定选项>11、指定选项</a><br>
              &nbsp;&nbsp; 字符&quot;|&quot;指定两个或多个选择来匹配模式。如:/def|ghi/匹配def或ghi。<br>
              &nbsp;&nbsp; 例:检验数字表示合法性<br>
              &nbsp;&nbsp; if ($number =~ /^-?\d+$|^-?0[xX][\da-fa-F]+$/) {<br>
              &nbsp;&nbsp;&nbsp;&nbsp; print (&quot;$number is a legal integer.\n&quot;);<br>
              &nbsp;&nbsp; } else {<br>
              &nbsp;&nbsp;&nbsp;&nbsp; print (&quot;$number is not a legal integer.\n&quot;);<br>
              &nbsp;&nbsp; }<br>
              &nbsp;&nbsp; 其中 ^-?\d+$ 匹配十进制数字,^-?0[xX][\da-fa-F]+$ 匹配十六进制数字。<br>
              <a name=12、模式的部分重用>12、模式的部分重用</a><br>
              &nbsp;&nbsp; 当模式中匹配相同的部分出现多次时,可用括号括起来,用\n来多次引用,以简化表达式:<br>
              /\d{2}([\W])\d{2}\1\d{2}/ 匹配:<br>
              &nbsp;&nbsp;&nbsp;&nbsp; 12-05-92<br>
              &nbsp;&nbsp;&nbsp;&nbsp; 26.11.87<br>
              &nbsp;&nbsp;&nbsp;&nbsp; 07 04 92等<font color="red">但不匹配12-05.92</font><br>
              &nbsp;&nbsp; 注意:/\d{2}([\W])\d{2}\1\d{2}/ 不同于/(\d{2})([\W])\1\2\1/ 
              ,后者只匹配形如17-17-17的字符串,而不匹配17-05-91等。<br>
              <a name=13、转义和特定字符的执行次序>13、转义和特定字符的执行次序</a><br>
              &nbsp;&nbsp; 象操作符一样,转义和特定字符也有执行次序:<br>
            </p>
            <table border=1 width=50%>
              <tr>
                <td valign=top width=157>特殊字符</td>
                <td valign=top width=198>描述</td>
              </tr>
              <tr>
                <td valign=top width=157><tt>()</tt></td>
                <td valign=top width=198>模式内存</td>
              </tr>
              <tr>
                <td valign=top width=157><tt>+ * ? {}</tt></td>
                <td valign=top width=198>出现次数</td>
              </tr>
              <tr>
                <td valign=top width=157><tt>^ $ \b \B</tt></td>
                <td valign=top width=198>锚</td>
              </tr>
              <tr>
                <td valign=top width=157><tt>|</tt></td>
                <td valign=top width=198>选项</td>
              </tr>
            </table>
            <br>
            <a name=14、指定模式定界符>14、指定模式定界符</a><br>
            &nbsp;&nbsp; 缺省的,模式定界符为反斜线/,但其可用字母m自行指定,如:<br>
            &nbsp;&nbsp;&nbsp;&nbsp; m!/u/jqpublic/perl/prog1! 等价于/\/u\/jqpublic\/perl\/prog1/<br>
            &nbsp;&nbsp; 注:当用字母'作为定界符时,不做变量替换;当用特殊字符作为定界符时,其转义功能或特殊功能即不能使用。<font color="red">成对的符号必须成对地使用。</font><br>
            <a name=15、模式次序变量>15、模式次序变量</a><br>
            &nbsp;&nbsp; 在模式匹配后调用重用部分的结果可用变量$n,全部的结果用变量$&amp;。<font color="red">匹配处之前的部分用变量$`,匹配处之前的部分用变量$'。也可用列表一次取得。</font><br>
            &nbsp;&nbsp;&nbsp;&nbsp; $string = &quot;This string contains the 
            number 25.11.&quot;;<br>
            &nbsp;&nbsp;&nbsp;&nbsp; $string =~ /-?(\d+)\.?(\d+)/; # 匹配结果为25.11<br>
            &nbsp;&nbsp;&nbsp;&nbsp; $integerpart = $1; # now $integerpart = 25<br>
            &nbsp;&nbsp;&nbsp;&nbsp; $decimalpart = $2; # now $decimalpart = 11<br>
            &nbsp;&nbsp;&nbsp;&nbsp; $totalpart = $&amp;; # now totalpart = 25.11<br>
            <font color="red">$_ = &quot;This string contains the number 25.11.&quot;;<br>
            &nbsp;&nbsp;&nbsp;&nbsp; @result =~ /-?(\d+)\.?(\d+)/;<br>
            </font> &nbsp;&nbsp;&nbsp;&nbsp; <br>
            <a name=四、模式匹配选项>四、模式匹配选项</a><br>
            <p></p>
            <a name=1、匹配所有可能的模式(g选项)>1、匹配所有可能的模式(g选项)</a><font color="red">不加g只匹配一处。</font><br>
            &nbsp;&nbsp; @matches = &quot;balata&quot; =~ /.a/g; # now @matches 
            = (&quot;ba&quot;, &quot;la&quot;, &quot;ta&quot;)<br>
            &nbsp;&nbsp; 匹配的循环:<br>
            &nbsp;&nbsp; while (&quot;balata&quot; =~ /.a/g) {<br>
            &nbsp;&nbsp;&nbsp;&nbsp; $match = $&amp;;<br>
            &nbsp;&nbsp;&nbsp;&nbsp; print (&quot;$match\n&quot;);<br>
            &nbsp;&nbsp; }<br>
            &nbsp;&nbsp; 结果为:<br>
            &nbsp;&nbsp;&nbsp;&nbsp; ba<br>
            &nbsp;&nbsp;&nbsp;&nbsp; la<br>
            &nbsp;&nbsp;&nbsp;&nbsp; ta<br>
            &nbsp;&nbsp; 当使用了选项g时,可用函数pos来控制下次匹配的偏移:<br>
            &nbsp;&nbsp;&nbsp;&nbsp; $offset = pos($string);<br>
            &nbsp;&nbsp;&nbsp;&nbsp; pos($string) = $newoffset;<br>
            <a name=2、忽略大小写(i选项)例>2、忽略大小写(i选项)例</a><br>
            &nbsp;&nbsp; /de/i 匹配de,dE,De和DE。<br>
            <a name=3、将字符串看作多行(m选项)>3、将字符串看作多行(m选项)</a><br>
            &nbsp;&nbsp; 在此情况下,^符号匹配字符串的起始或新的一行的起始;$符号匹配任意行的末尾。<br>
            <a name=4、只执行一次变量替换例>4、只执行一次变量替换例</a><br>
            &nbsp;&nbsp; $var = 1;<br>
            &nbsp;&nbsp; $line = &lt;STDIN&gt;;<br>
            &nbsp;&nbsp; while ($var &lt; 10) {<br>
            &nbsp;&nbsp;&nbsp;&nbsp; $result = $line =~ /$var/o;<br>
            &nbsp;&nbsp;&nbsp;&nbsp; $line = &lt;STDIN&gt;;<br>
            &nbsp;&nbsp;&nbsp;&nbsp; $var++;<br>
            &nbsp;&nbsp; }<br>
            &nbsp;&nbsp; 每次均匹配/1/。<br>
            <a name=5、将字符串看作单行例>5、将字符串看作单行例</a><br>
            &nbsp;&nbsp; /a.*bc/s匹配字符串axxxxx \nxxxxbc,但/a.*bc/则不匹配该字符串。<br>
            <a name=6、在模式中忽略空格>6、在模式中忽略空格</a><br>
            &nbsp;&nbsp; /\d{2} ([\W]) \d{2} \1 \d{2}/x等价于/\d{2}([\W])\d{2}\1\d{2}/。<br>
            <a name=五、替换操作符>五、替换操作符</a><br>
            &nbsp;&nbsp; 语法为s/pattern/replacement/,其效果为将字符串中与pattern匹配的部分换成replacement。如:<br>
            &nbsp;&nbsp;&nbsp;&nbsp; $string = &quot;abc123def&quot;;<br>
            &nbsp;&nbsp;&nbsp;&nbsp; $string =~ s/123/456/; # now $string = &quot;abc456def&quot;;<br>
            &nbsp;&nbsp; 在替换部分可使用模式次序变量$n,如s/(\d+)/[$1]/,但在替换部分不支持模式的特殊字符,如{},*,+等,如s/abc/[def]/将把abc替换为[def]。<br>
            &nbsp;&nbsp; 替换操作符的选项如下表:<br>
            <p></p>
            <table border=1 width=60%>
              <tr>
                <td valign=top width=91>选项</td>
                <td valign=top width=499>描述</td>
              </tr>
              <tr>
                <td valign=top width=91><tt>g</tt></td>
                <td valign=top width=499>改变模式中的所有匹配</td>
              </tr>
              <tr>
                <td valign=top width=91><tt>i</tt></td>
                <td valign=top width=499>忽略模式中的大小写</td>
              </tr>
              <tr>
                <td valign=top width=91><tt>e</tt></td>
                <td valign=top width=499>替换字符串作为表达式</td>
              </tr>
              <tr>
                <td valign=top width=91><tt>m</tt></td>
                <td valign=top width=499>将待匹配串视为多行</td>
              </tr>
              <tr>
                <td valign=top width=91><tt>o</tt></td>
                <td valign=top width=499>仅赋值一次</td>
              </tr>
              <tr>
                <td valign=top width=91><tt>s</tt></td>
                <td valign=top width=499>将待匹配串视为单行</td>
              </tr>
              <tr>
                <td valign=top width=91><tt>x</tt></td>
                <td valign=top width=499>忽略模式中的空白</td>
              </tr>
            </table>
            <p>&nbsp;&nbsp; 注:e选项把替换部分的字符串看作表达式,在替换之前先计算其值,如:<br>
              &nbsp;&nbsp;&nbsp;&nbsp; $string = &quot;0abc1&quot;;<br>
              &nbsp;&nbsp;&nbsp;&nbsp; $string =~ s/[a-zA-Z]+/$&amp; x 2/e; # 
              now $string = &quot;0abcabc1&quot;<br>
              <a name=六、翻译操作符>六、翻译操作符</a><br>
              &nbsp;&nbsp; 这是另一种替换方式,语法如:tr/string1/string2/。同样,string2为替换部分,但其效果是把string1中的第一个字符替换为string2中的第一个字符,把string1中的第二个字符替换为string2中的第二个字符,依此类推。如:<br>
              &nbsp;&nbsp;&nbsp;&nbsp; $string = &quot;abcdefghicba&quot;;<br>
              &nbsp;&nbsp;&nbsp;&nbsp; $string =~ tr/abc/def/; # now string = 
              &quot;defdefghifed&quot;<br>
              &nbsp;&nbsp; 当string1比string2长时,其多余字符替换为string2的最后一个字符;当string1中同一个字符出现多次时,将使用第一个替换字符。<br>
              &nbsp;&nbsp; 翻译操作符的选项如下:<br>
            </p>
            <table border=1 width=80%>
              <tr>
                <td valign=top width=69>选项</td>
                <td valign=top width=447>描述</td>
              </tr>
              <tr>
                <td valign=top width=69><tt>c</tt></td>
                <td valign=top width=447>翻译所有未指定字符</td>
              </tr>
              <tr>
                <td valign=top width=69><tt>d</tt></td>
                <td valign=top width=447>删除所有指定字符</td>
              </tr>
              <tr>
                <td valign=top width=69><tt>s</tt></td>
                <td valign=top width=447>把多个相同的输出字符缩成一个</td>
              </tr>
            </table>
            <p>&nbsp;&nbsp; 如$string =~ tr/\d/ /c;把所有非数字字符替换为空格。$string =~ tr/\t 
              //d;删除tab和空格;&nbsp; $string =~ tr/0-9/ /cs;把数字间的其它字符替换为一个空格。<br>
              <font color="red">在替换中使用字符类型<br>
              $_=&quot;this is a test&quot;;<br>
              s/(\w+)/&lt;$1&gt;/g; # $_ is now &quot;&lt;this&gt; &lt;is&gt; 
              &lt;a&gt; &lt;test&gt;&quot;</font><br>
              <br>
              <a name=七、扩展模式匹配>七、扩展模式匹配</a><br>
              &nbsp;&nbsp; PERL支持PERL4和标准UNIX模式匹配操作所没有的一些模式匹配能力。其语法为:(?&lt;c&gt;pattern),其中c是一个字符,pattern是起作用的模式或子模式。<br>
              <a name=1、不存贮括号内的匹配内容>1、不存贮括号内的匹配内容</a><br>
              &nbsp;&nbsp; 在PERL的模式中,括号内的子模式将存贮在内存中,此功能即取消存贮该括号内的匹配内容,如/(?:a|b|c)(d|e)f\1/中的\1表示已匹配的d或e,而不是a或b或c。<br>
              <a name=2、内嵌模式选项>2、内嵌模式选项</a><br>
              &nbsp;&nbsp; 通常模式选项置于其后,有四个选项:i、m、s、x可以内嵌使用,语法为:/(?option)pattern/,等价于/pattern/option。<br>
              <a name=3、肯定的和否定的预见匹配>3、肯定的和否定的预见匹配<br>
              </a>&nbsp;&nbsp; 肯定的预见匹配语法为/pattern(?=string)/,其意义为匹配后面为string的模式,相反的,(?!string)意义为匹配后面非string的模式,如:<br>
              &nbsp;&nbsp;&nbsp;&nbsp; $string = &quot;25abc8&quot;;<br>
              &nbsp;&nbsp;&nbsp;&nbsp; $string =~ /abc(?=[0-9])/;<br>
              &nbsp;&nbsp;&nbsp;&nbsp; $matched = $&amp;; # $&amp;为已匹配的模式,此处为abc,而不是abc8<br>
              <a name=4、模式注释>4、模式注释</a><br>
              &nbsp;&nbsp; PERL5中可以在模式中用?#来加注释,如:<br>
              &nbsp;&nbsp;&nbsp;&nbsp; if ($string =~ /(?i)[a-z]{2,3}(?# match 
              two or three alphabetic characters)/ {<br>
              &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...<br>
              &nbsp;&nbsp;&nbsp;&nbsp; }<br>
              <br>
            </p>
            <p align=center><a href=perl5.htm>上一章</a> <a href=perl7.htm>下一章</a> 
              <a href=index.htm>目录</a></p>
            <p align=center>&nbsp; </p>
            <!-- #EndEditable --></td>
        </tr>
      </table>
    </td></tr></table></body><!-- #EndTemplate --></html>

⌨️ 快捷键说明

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