Here’s the idea using the Perl 3rd-party Regexp::Assemble module from CPAN. For example, given this program:
#!/usr/bin/env perl
use utf8;
use strict;
use warnings;
use Regexp::Assemble;
my %faces = (
HAPPY => [qw? :-) :) :o) :-} ;-} :-> ;-} ?],
SAD => [qw? :-( :( :-| ;-) ;-( ;-< |-{ ?],
);
for my $name (sort keys %faces) {
my $ra = Regexp::Assemble->new();
for my $face (@{ $faces{$name} }) {
$ra->add(quotemeta($face));
}
printf "%-12s => %s
", "[$name]", $ra->re;
}
It will output this:
[HAPPY] => (?-xism:(?::(?:-(?:[)>]|})|o?))|;-}))
[SAD] => (?-xism:(?::(?:-(?:||()|()|;-[()<]||-{))
There’s a bit of extra stuff there you don’t really probably need, so those would reduce to just:
[HAPPY] => (?:-(?:[)>]|})|o?))|;-}
[SAD] => (?:-(?:||()|()|;-[()<]||-{
or so. You could build that into your Perl program to trim the extra bits. Then you could place the righthand sides straight into your preg_replace
.
The reason I did the use utf8
was so I could use ?
as my qw//
delimiter, because I didn’t want to mess with escaping things inside there.
You wouldn’t need to do this if the whole program were in Perl, because modern versions of Perl already know to do this for you automatically. But it’s still useful to know how to use the module so you can generate patterns to use in other languages.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…