in reply to Re: Combining multiple =~ s/
in thread Combining multiple =~ s/

You want to sort the words in the regular expression by descending length :) Or alternatively, use \b to match only whole words:

my %trans = (zero => 0, one => 1, ones => 11, two => 2, three => 3); my $words_re = join "|", map quotemeta, sort { length($b) <=> length($ +a) } keys %trans; $string =~ s/\b($words_re)\b/$trans{lc $1}/ig;

Otherwise, it could be that one matches before ones.

Update: choroba spotted that the length($b) and length($a) were missing

Replies are listed 'Best First'.
Re^3: Combining multiple =~ s/
by no longer just digit (Beadle) on Mar 10, 2021 at 06:54 UTC

    It might be worth pointing out the CPAN module Data::Munge which has a function list2re to make a list-matching regex. This line is basically what you need:

    my $re = join '|', map quotemeta, sort {length $b <=> length $a || +$a cmp $b } @_;

    There is also this, which is what I use.