in reply to Re: Comparing pattern
in thread Comparing pattern

Thanks a lot guys!!! Your both suggestions helped me to solve this problem. Based on bv suggestion regarding qr//, I also found a great post: http://www.perlmonks.org/?node_id=661292. That Regexp::Assemble do a great job.

Replies are listed 'Best First'.
Re^3: Comparing pattern
by mrc (Sexton) on Sep 19, 2009 at 10:15 UTC
    Regexp::Assemble would not track the original pattern correctly. I give up, it's too hard for me :(
    use Regexp::Assemble; my $patterns = "/path/to/file.txt"; my $list_regex = Regexp::Assemble->new(file => $patterns); $list_regex->track( 1 ); open( FILE, "<", "$arg1") or die "$arg1: $!\n"; while (<FILE>) { if (/$list_regex/) {print "\n$arg1\n$list_regex->matched\n";} } close(FILE); }
    Now I have this code but I'm facing a new problem. In my first example I use both flags /is I need /s so . to match newlines as well. I have this
    my $patterns = "/path/to/file.txt"; my $arg1 = shift; open( PATTERNS, "<", $patterns ) or die "$patterns: $!\n"; my @list_patterns = <PATTERNS>; close PATTERNS; chomp @list_patterns; my $regexStr = "(" . join("|", @list_patterns) . ")"; my $list_regex = qr{$regexStr}i; open( FILE, "<", "$arg1") or die "$arg1: $!\n"; while (<FILE>) { if (/$list_regex/) {print "\n$arg1\n$1\n";} } close(FILE);

    Adding s to both
    my $list_regex = qr{$regexStr}is; or
    if (/$list_regex/is)
    would not solve the problem.
    part1.*part2
    This pattern working with my original script. .* should match also newline.
    fggffgfg part1 hghggh ghhggh hggh part2 ytyty
    This is the last problem, else the script working perfectly and much faster thanks to your advices. I will next take a look at local $/.
      You say that you "need /s so . to match newlines as well", and I assume this means that if your pattern list includes something like foo.*?bar, you would want that to match either of the following examples (where I'll put parens around the intended match):
      example 1: blah (foobar) blah example 2: blah (foo all sorts of content on lots of lines of data bar) blah
      In that case, you need to read the data file in slurp mode:
      my $regex_string = join '|', @list_patterns; open( FILE, "<", $arg1 ) or die "$arg1: $!"; $_ = do { local $/; <FILE> }; # temporarily set $/ = undef to slurp f +ile close FILE; if ( /($regex_string)/is ) { # got a match... }
      Regarding the placement of parens and regex qualifiers, you could do that in other ways, like:
      my $list_regex = qr/($regex_string)/is; ... if ( /$list_regex/ ) { ...
      No difference, really, but when there's a choice, I like having the parens visible in the code near the place where I use $1, $2, etc.