in reply to Idiom for a regex translation

use Regexp::Common 'pattern'; pattern name => ['squish'], create => '\W+'; my $name = "Rob J Anderson"; my $squished_name; $squished_name = $RE{squish}->subs($name); # Following only needed because of /g: $squished_name = $RE{squish}->subs($squished_name) while $RE{squish}->matches($squished_name);

Replies are listed 'Best First'.
Re^2: Idiom for a regex translation
by reasonablekeith (Deacon) on Apr 22, 2005 at 15:31 UTC
    I tried to run this, but it didn't do anything (the string was unmodified). A quick read later, and I came up with this...
    use Regexp::Common 'pattern'; pattern name => ['squish'], create => '.', subs => sub { $_[1] =~ s/\W+//g; }; my $name = "Rob J Anderson"; my $squished_name = $RE{squish}->subs($name); print "name :$name\n"; print "squished name :$squished_name\n"; __OUTPUT__ name :Rob J Anderson squished name :RobJAnderson
    Which fulfils all my requirements. I'm not sure about the 'create' parameter though. It seemed mandatory, but I can't see that it gets used in this context.

    Anyway, thanks for all the responses.