in reply to convert string into a match pattern
In addition to quotemeta, note also that if you want to match many strings against each line in a file, it's much more efficient to combine all the strings into a single pattern, so something like the following:
In the code above I only convert numbers into \d+ if preceded by '='. I've also added ^ and $ anchors to the pattern, which you may or may not want.my @strings = ( 'nnxx.yy2 = 234 abc', 'foo bar = 39 baz *', .... ); my $pattern = join '|', map { my $s = quotemeta; $s =~ s/(=\s*)\d+/$1\\d+/g; $s } @strings +; my $qr = qr/^($pattern)$/; while (<>) { print if /$qr/; }
Dave.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: convert string into a match pattern
by Anonymous Monk on Dec 22, 2017 at 07:30 UTC | |
by dave_the_m (Monsignor) on Dec 22, 2017 at 09:05 UTC |
In Section
Seekers of Perl Wisdom