yh924
新手求助,关于PERL基础教程中习题的疑惑,望高手解答
PERL 语言入门第四版
8.10
3. [5]修改第二题的程序,使之将由a 结尾的单词放到$1 之中。同时修改源代码,使此变量对应的值被放在单引号之中,
如$1 contains ‘Wilma’。
4. [5]额外练习:修改第三题程序,使之能捕捉由a 结尾的单词之后的5 个字符(如果有那么多),并将之放入一个独立
变量中。例如,如果输入的是I saw Wilma yesterday, 则紧接的5 个字符是yest(前有空格)。如果输入是I, Wilma!,
则只有一个字符。它现在还能匹配wilma 吗?
上面是两道习题
3.Here's one way to do it:
#!/usr/bin/perl
while (<STDIN>) {
chomp;
if (/(/b/w*a/b)/) {
print "Matched: |$`<$&>$'|/n";
print "/$1 contains '$1'/n"; # The new output line
} else {
print "No match: |$_|/n";
}
}
这个调试通过
4.Here's one way to do it:
m!
(/b/w*a/b) # $1: a word ending in a
(.{0,5}) # $2: up to five characters following
!xs # /x and /s modifiers
这个东西就不太懂了。这个东西怎么加到上面的例子中呢?
哪位达人可以帮忙调试一下呢?谢谢了。
不死草
回复 #1 yh924 的帖子
其实就是条件,你直接应用就可以了!~:mrgreen:
[code]
#!/usr/bin/perl
while (<STDIN>) {
chomp;
if (m!
(/b/w*a/b) # $1: a word ending in a
(.{0,5}) # $2: up to five characters following
!xs # /x and /s modifiers
){
print "Matched: |$`<$&>$'|/n";
print "/$1 contains '$1'/n"; # The new output line
print "/$2 contains '$2'/n"; # The new output line
} else {
print "No match: |$_|/n";
}
}
[/code]