in reply to User configurable regex
Here is my two cents' worth. I am not sure if it is what you mean. I have made the regexes deliberately somewhat convoluted :)
#! /usr/bin/perl -w use warnings; use diagnostics; use strict; eval { action() }; if ($@) { print "error:\n$@\n"; } sub action { my @a = split(/\n/,<<'ENDTEXT'); This is the text we will search through. Normally you'd have some other source like a file. Every line here becomes an element in array @a. ENDTEXT my $rregexes = get_regexes(); # get patterns my @compiled = map qr/$_/ix, @$rregexes; # pre-compile them, # case insensitive print "We got ". @$rregexes ." regexes to test:\n"; # show regexes: for (my $i=0;$i< @$rregexes ;$i++) { print "--------\nregex $i:\n--------\n"; print $rregexes->[$i]; } print "\nHere we go into matching loop:\n"; for (my $i=0;$i<@a;$i++) { for (my $j=0;$j<@compiled;$j++) { if ($a[$i] =~ /$compiled[$j]/) { print "$j match: ".sprintf('%-50s',$a[$i])."<== matched\n"; } else { print "$j no match: ".sprintf('%-50s',$a[$i])."\n"; } } } } sub get_regexes { # # Could also read these from config files! # my @regexes = (); my $i = -1; $regexes[++$i] =<<'REGEXTEXT'; ^ # beginning of string .* # any number of chars normally # .* # $ # end REGEXTEXT $regexes[++$i] =<<'REGEXTEXT'; ^ # beginning of string .* # any number of chars here # .* # element # .* # $ # end REGEXTEXT return \@regexes; }
|
|---|