LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1459|回复: 4

请解释一下这个脚本

[复制链接]
发表于 2003-11-30 23:59:30 | 显示全部楼层 |阅读模式
[php]
#!perl -w
while(<>) { #<>表示从键盘读取一个数值,也可以这样写<STDIN>
        chomp;#截去回车这个操作符,手写输入的数据不变。
        if (/string/) {    #string表示你想要匹配的模式
                print "Matched:|$`<$&>$'|\n";
                } else {
                print "No match.\n";
        }
}
[/php]
试试运行一下,然后请给个注释~~ ;)
发表于 2003-12-2 15:59:35 | 显示全部楼层
#!perl -w
#这行是不是有错误,Perl程序的路径,好象应该是#!/usr/bin/perl -w,windows上不需要

while(<> ) {
#<>表示从键盘读取输入的数据,<>是输入操作符,缺省为STDIN
        chomp;
           #截去\n,等价为 chomp $_; Perl中的所有函数的默认参数可能为 $_,@_等
        if (/string/) {     
           #正则表达式,相当于$_ ~= /string/m;
                print "Matched:|$`<$&>$'|\n";
                           #最复杂的就是这里了
                } else {
                print "No match.\n";
        }
}

最后面几行,看文档中的介绍,特殊变量
$PREMATCH

$`

The string preceding whatever was matched by the last successful pattern match (not counting any matches hidden within a BLOCK or eval enclosed by the current BLOCK). (Mnemonic: ` often precedes a quoted string.) This variable is read-only.
The use of this variable anywhere in a program imposes a considerable performance penalty on all regular expression matches.

$POSTMATCH

$'

The string following whatever was matched by the last successful pattern match (not counting any matches hidden within a BLOCK or eval() enclosed by the current BLOCK). (Mnemonic: ' often follows a quoted string.) Example:
    local $_ = 'abcdefghi';
    /def/;
    print "$`&'\n";         # prints abc:def:ghiThis variable is read-only and dynamically scoped to the current BLOCK.

The use of this variable anywhere in a program imposes a considerable performance penalty on all regular expression matches.

$MATCH

$&

The string matched by the last successful pattern match (not counting any matches hidden within a BLOCK or eval() enclosed by the current BLOCK). (Mnemonic: like & in some editors.) This variable is read-only and dynamically scoped to the current BLOCK.
The use of this variable anywhere in a program imposes a considerable performance penalty on all regular expression matches.
发表于 2003-12-2 16:03:07 | 显示全部楼层
一句话:
$`为匹配文字前面的内容
$&为匹配文字的内容
$'为匹配文字后面的内容
 楼主| 发表于 2003-12-2 16:33:11 | 显示全部楼层
最初由 tojeff 发表
一句话:
$`为匹配文字前面的内容
$&为匹配文字的内容
$'为匹配文字后面的内容

精辟~~~
发表于 2003-12-2 23:33:39 | 显示全部楼层
呵呵~~~~歡迎高手?磉@裡指教, 唉~~~我全是灌水了. 呵呵~~~~ :p
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表