Can anyone see a good way to load the RegExes from an eternal file?
There is probably a module for this somewhere, but if not, something along the lines of the following may serve. Your search patterns all seem to be plain octal sequences of various lengths and the replacement strings character string literals, so that simplifies things. I assume from your OP that you're confident about reading raw string data from a file and massaging it appropriately, so:
c:\@Work\Perl>perl -wMstrict -le "use Data::Dump qw(dd); ;; my @filelines = ( '\102\105\105 B', '\104\125\102\131\101 W', '\130 EKS', '\132 ZEE', ); ;; my %search_terms = map split, @filelines; ;; my %xlate = map { eval(qq{ qq{$_} }) => $search_terms{$_} } keys %search_terms ; dd \%xlate; ;; my ($search) = map qr{ $_ }xms, join ' | ', reverse sort keys %search_terms ; print $search; ;; my $s = 'the BEEroDUBYAn foX jumped the laZy dog'; print qq{'$s'}; ;; $s =~ s{ ($search) }{$xlate{ $1 }}xmsg; print qq{'$s'}; " { BEE => "B", DUBYA => "W", X => "EKS", Z => "ZEE" } (?^msx: \132 | \130 | \104\125\102\131\101 | \102\105\105 ) 'the BEEroDUBYAn foX jumped the laZy dog' 'the BroWn foEKS jumped the laZEEy dog'
Some other more or less random thoughts:
Perl could have warned you about this had you had warnings enabled.c:\@Work\Perl>perl -wMstrict -le "my $s = '>nbsp< hi nbsp there'; print qq{'$s'}; ;; $s =~ s{ nbsp }{\s}xmsg; print qq{'$s'}; " Unrecognized escape \s passed through at -e line 1. '>nbsp< hi nbsp there' '>s< hi s there'
This is only cosmetic, but this could be much simpler and IMHO clearer in consequence. The s/// operator does not do substitutions if the pattern is not matched, and can be usedif($line =~ /[^[:ascii:]]/){ $line =~ s/([^[:ascii:]])/ ... lotsa subexpressions ... /ge; print "Unhandled sequence: $line\n"; }
c:\@Work\Perl>perl -wMstrict -e "my $s = 'aXbYcZd'; print qq{'$s' \n}; ;; print qq{unhandled sequence: '$s' \n} if $s =~ s{ ([XYZ]) }{ sprintf '[%d/%1$#X/%1$#o]', ord $1 }xmsge; " 'aXbYcZd' unhandled sequence: 'a[88/0X58/0130]b[89/0X59/0131]c[90/0X5A/0132]d'
Update:
$line =~ s/\302\240/\s/g; # nbsp to sp
So, I guess this raises the question of just how you get a literal space from a space-delimited file to use as a replacement string, anyway? One way would be to use a non-space delimiter, but this may just shift the problem to another boundary. Another approach would be to specify all such characters in their octal equivalent, \040 and so on. This sets us upon the the long and winding road of s///ee and template processing (pursue the links from Re: Evaluating $1 construct in literal replacement expression). In any event, here's one way:
c:\@Work\Perl>perl -wMstrict -le "use Data::Dump qw(dd); ;; my @filelines = ( '\102\105\105 B', 'nbsp \040', '\130 EKS', '\132 ZEE', ); ;; my %search_terms = map split, @filelines; ;; my %xlate = map { eval(qq{ qq{$_} }) => $search_terms{$_} } keys %search_terms ; dd \%xlate; ;; my ($search) = map qr{ $_ }xms, join ' | ', reverse sort keys %search_terms ; print $search; ;; my $s = 'the BEErownbspfoX >nbsp< the laZy dog'; print qq{'$s'}; ;; $s =~ s{ ($search) }{ qq{ qq{$xlate{$1}} } }xmsgee; print qq{'$s'}; " { BEE => "B", nbsp => "\\040", X => "EKS", Z => "ZEE" } (?^msx: nbsp | \132 | \130 | \102\105\105 ) 'the BEErownbspfoX >nbsp< the laZy dog' 'the Brow foEKS > < the laZEEy dog'
Give a man a fish: <%-{-{-{-<
In reply to Re: Read RegEx from file
by AnomalousMonk
in thread Read RegEx from file
by Melly
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |