yorkwu has asked for the wisdom of the Perl Monks concerning the following question:
Content of file "input.data"#!/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\]
The expected result is:foo/bar\[1\] 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.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\]
But it still doesn't match!my $t = 'some path/to/foo/bar\[1\]\[2\]'; my $a = 'bar\\[1'; print "match!\n" if($t =~ m/$a/);
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Regexp pattern match problem
by Samy_rio (Vicar) on Aug 20, 2007 at 07:09 UTC |