
winfinit
User
Dec 1, 2008, 5:06 AM
Post #2 of 2
(5514 views)
|
Re: [shaybery] How to match "key=value #comment" ?
[In reply to]
|
Can't Post
|
|
first inside your if statement you have = and not =~ so that is one of the reasons why it is not working, second, it is not good to use .* because they are greedy so if you want to match anything up until specific character, then use ? after so .*? and then if you know that your line would always begin with your defined character, then use ^. hope that helps :) chomp(my $line = <STDIN>); if ($line =~ /^(.*?)=(.*?)(#.*)/){ my ($val,$key,$comment)=($1,$2,$3); print "Match line : key($key)=val($val) c($comment) \n"; } winfinit-2:~ winfinit$ perl test.pl a=b #c Match line : key(b )=val(a) c(#c) winfinit-2:~ winfinit$ something is weird with output, but you will figure it out :)
|