[问题]关于Perl的模式匹配

软件和网站开发以及相关技术探讨
回复
duanmu1123
帖子: 5
注册时间: 2008-07-22 21:11

[问题]关于Perl的模式匹配

#1

帖子 duanmu1123 » 2008-07-26 9:18

请问各位高手 , 在PERL模式匹配中,如果要匹配的字符中含有括号,怎么匹配?
例如:
my $fileName = 'c:\dtest\hell (2) copy.cpp';
my $pattern = 'hell (2) copy.cpp';

print $fileName if($fileName =~ m/$pattern/g);

这样是匹配不到,请问有什么办法没有?
谢谢~ :)
头像
zcg0696
帖子: 44
注册时间: 2008-05-27 17:30

#2

帖子 zcg0696 » 2008-07-27 0:00

\(\)试试看,不过为什么要起这么怪的名字。。。
duanmu1123
帖子: 5
注册时间: 2008-07-22 21:11

#3

帖子 duanmu1123 » 2008-07-27 0:04

模式里是用变量名来匹配里的,不能转义
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

#4

帖子 eexpress » 2008-07-27 0:35

The use of "\Q" causes the <.> in the regex to be treated as a regular character, so that
"P." matches a "P" followed by a dot.

变量名前面可以带一些“转义”的。

具体的,可以去irc的perl房间问。
● 鸣学
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

#5

帖子 eexpress » 2008-07-27 0:39

or
man perlfaq6
● 鸣学
duanmu1123
帖子: 5
注册时间: 2008-07-22 21:11

[已解决]

#6

帖子 duanmu1123 » 2008-07-27 6:19

eexpress 写了:The use of "\Q" causes the <.> in the regex to be treated as a regular character, so that
"P." matches a "P" followed by a dot.

变量名前面可以带一些“转义”的。

具体的,可以去irc的perl房间问。
非常有效果!
谢谢了,高手! 能不能问下,上面这段是在哪看到的? :D
heejun
帖子: 60
注册时间: 2006-05-01 12:29
来自: zju

Re: [已解决]

#7

帖子 heejun » 2008-07-29 18:48

duanmu1123 写了:
eexpress 写了:The use of "\Q" causes the <.> in the regex to be treated as a regular character, so that
"P." matches a "P" followed by a dot.

变量名前面可以带一些“转义”的。

具体的,可以去irc的perl房间问。
非常有效果!
谢谢了,高手! 能不能问下,上面这段是在哪看到的? :D

代码: 全选

perldoc -f quotemeta
头像
Tobey
帖子: 178
注册时间: 2006-03-30 13:34

#8

帖子 Tobey » 2008-07-29 22:06

some.txt内容

d32=notebook
d35=powerpc

请问如何匹配。
my $model="d32";
open(env, ">some.txt");
while(<env>)
{
if($_ =~ /^$model=(.*)/)
{
$product=$1;
}
}
close(env);
print "$product";


如果把$model换成字符可以。但变量匹配不到。
duanmu1123
帖子: 5
注册时间: 2008-07-22 21:11

#9

帖子 duanmu1123 » 2008-08-02 10:28

匹配得到啊,你再试试,
回复