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

In reply to Regexp pattern match problem by yorkwu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.