Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Regexp pattern match problem

by yorkwu (Novice)
on Aug 20, 2007 at 06:06 UTC ( [id://633712]=perlquestion: print w/replies, xml ) Need Help??

yorkwu has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I was trying to write a script to know if the pattern in file "input.data" match __DATA__. The script as bellow.
#!/usr/bin/perl use strict; use warnings; open FIN, "<input.data" or die $!; my @line = <FIN>; close FIN; while(<DATA>){ chomp; my $data = $_; for my $line (@line){ chomp($line); if($data =~ m/$line/){ print "$line +++++++++ $data\n" }else{ print "$line --------- $data\n" } } } __DATA__ some path/to/foo/bar\[1\]\[2\] thing apple/flower\[7\] gogo dog/cat\[9\]\[8\]
Content of file "input.data"
foo/bar\[1\] dog/cat\[9\]\[8\]
The expected result is:
foo/bar\[1\] +++++++++ some path/to/foo/bar\[1\]\[2\] dog/cat\[9\]\[8\] --------- some path/to/foo/bar\[1\]\[2\] foo/bar\[1\] --------- thing apple/flower\[7\] dog/cat\[9\]\[8\] --------- thing apple/flower\[7\] foo/bar\[1\] --------- gogo dog/cat\[9\]\[8\] dog/cat\[9\]\[8\] +++++++++ gogo dog/cat\[9\]\[8\]
The actual result is:
foo/bar\[1\] --------- some path/to/foo/bar\[1\]\[2\] dog/cat\[9\]\[8\] --------- some path/to/foo/bar\[1\]\[2\] foo/bar\[1\] --------- thing apple/flower\[7\] dog/cat\[9\]\[8\] --------- thing apple/flower\[7\] foo/bar\[1\] --------- gogo dog/cat\[9\]\[8\] dog/cat\[9\]\[8\] --------- gogo dog/cat\[9\]\[8\]
I was guessing maybe I should add "\" to deal with "\" in the file "input.data". So I did bellow test.
my $t = 'some path/to/foo/bar\[1\]\[2\]'; my $a = 'bar\\[1'; print "match!\n" if($t =~ m/$a/);
But it still doesn't match!
I'm confused. I should mess up some basic concept.
Could anyone give me a hand? Thanks in advance!
York

Replies are listed 'Best First'.
Re: Regexp pattern match problem
by Samy_rio (Vicar) on Aug 20, 2007 at 07:09 UTC

    Hi, Use \Q..\E quotemeta while checking the pattern

    #!/usr/bin/perl use strict; use warnings; open FIN, "<C:\\input.data" or die $!; my @line = <FIN>; close FIN; while(<DATA>){ chomp; my $data = $_; for my $line (@line){ chomp($line); if($data =~ m/\Q$line\E/){ print "$line +++++++++ $data\n" }else{ print "$line --------- $data\n" } } } __DATA__ some path/to/foo/bar[1][2] thing apple/flower[7] gogo dog/cat[9][8] input.data ---------- foo/bar[1] dog/cat[9][8] Output ------ foo/bar[1] +++++++++ some path/to/foo/bar[1][2] dog/cat[9][8] --------- some path/to/foo/bar[1][2] foo/bar[1] --------- thing apple/flower[7] dog/cat[9][8] --------- thing apple/flower[7] foo/bar[1] --------- gogo dog/cat[9][8] dog/cat[9][8] +++++++++ gogo dog/cat[9][8]

    Not necessary to escape the special character in input file.

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://633712]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-25 17:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found