CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
How to match "key=value #comment" ?

 



shaybery
New User

Nov 19, 2008, 4:09 PM

Post #1 of 2 (2131 views)
How to match "key=value #comment" ? Can't Post

This regular expresion is not good !!!
---------------------
if ($line=/(.*)=(.*)(#+.*)/){
my ($val,$key,$comment)=($1,$2,$3);
print "Match line : key($key)=val($val) c($comment) \n";

}
---------------------
Example os inputs and the wanted output :
a=b #c => key(a)=val(b) c(#c)
a=b => key(a)=val(b) c()
a="b#" => key(a)=val("b#") c()
a="b#"#ff => key(a)=val("b#") c(#ff)
a="b"#"#ff => key(a)=val("b") c(#"#ff)

Some one can help?


winfinit
User

Dec 1, 2008, 5:06 AM

Post #2 of 2 (1916 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 :)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives