only first pattern found is displayed now. That's not a problem, but I'd also like to know how to display all patterns if a file contains more than one.

That's easy -- instead of using an "if" statement like this:

if (/($regex_string)/is) {
just use a while loop like this -- making sure to add the "g" modifier (and while I'm at it, I'll add some clarification to the output):
while (/$regex_string)/isg) { print "\nmatched in $arg1:\n==$1==\n"; }
As for your other issue:

$1 will show all wildcarded text between part1 and part2 and not only the pattern part1.*part2 as it should.

What makes you think it "should" display the string "part1.*part2"? When using the capture variables ($1, $2, ...), the normal situation is to want the actual (complete, literal) string that matched the regex, rather than the regex string with its wildcards.

If you want the wildcard-enabled regexes in your list to return a specific constant string, you'll probably want to include that string in your regex list file, store those replacement strings with their regexes in a separate hash, and add some logic in the while loop shown above that will replace any given matching string with the appropriate constant replacement string. Here's an adapted version the three files involved:

First, the regex pattern list -- I'm using a tab character as a field delimiter between the "regex" column (on left) and the "constant-replacement" column (on the right). Trust me, this is the easiest way to get what you want:
part1.*?part2 part1.*?part2 Foo bar Other pattern
Here's a slightly more elaborate test file:
hghghgghghh part1 fff part2 blah blah Foo bar more noise Other pattern another match for part1 stuff I don't want to see part2 blabla
And here's the updated code:
#!/usr/bin/perl -w use strict; my $patterns = "patterns.txt"; my $arg1 = shift; my $regex_string = ''; my %patterns; open (PAT, '<', $patterns) or die "$patterns: $!\n"; while (<PAT>) { chomp; my ( $pattern, $replace ) = split '\t'; $patterns{$pattern} = $replace if ( $replace ); $regex_string .= $pattern . '|'; } close(PAT); chop $regex_string; # remove string-final '|'; open( FILE, "<", "$arg1") or die "$arg1: $!\n"; $_ = do { local $/; <FILE> }; close(FILE); while ( /($regex_string)/gis ) { my $match = $1; for my $replc ( keys %patterns ) { last if ( $match =~ s/$replc/$patterns{$replc}/is ); } print "\nmatched in $arg1:\n==$match==\n"; }

In reply to Re^5: Comparing pattern by graff
in thread Comparing pattern by mrc

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.