in reply to Orthography Translation using Regex

my $translation_table = { 'A' => 'A', # translate from => to 'a' => 'a', 'r' => 'r', # etc }; my $str = "Barra O Gri obhtha"; # build search pattern as 'pattern1|pattern2|...' my $patterns = join '|', keys %$translation_table; $str =~ s/($patterns)/$translation_table->{$1}/ge;

Replies are listed 'Best First'.
Re: Re: Orthography Translation using Regex
by ysth (Canon) on Feb 29, 2004 at 20:57 UTC
    When doing join '|', for a regex, always sort (descending) by string length, to insure you match e.g. 'sh' before 's'.

    And I suspect you want a map quotemeta in there as well. (Update: quotemeta isn't needed here; I misremembered the question as having special characters.)

      I am just giving an idea on how this could be done, not on the complete correctness of the regex though. :-)