in reply to Regexp and transliteration between languages

This will give you the chunks of a string:
@tokens = sort { length $b <=> length $a } qw(dny kh k h);
$re     = join "|", @tokens;
@chunks = split /($re)/, $text;
I'm building a regular expression that matches what you're looking for. I use the fact that Perl's RE engine tries alternated choices ("food|foo") from left to right, so I put the longest bits first. While the split() function ordinarily returns only the bits of the string the don't match the regular expression, by parenthesizing portions of the regexp, those portions are also returned.

If you have the correspondences, though, you can do a transliterator quite easily:

%changes = ( dny => 225,
             kh  => 35,
             k   => 12, # ...
);
$re = join "|", sort { length $b <=> length $a }
                keys %changes;
$text =~ s/($re)/chr $changes{$1}/ge;
I build a regular expression from the keys of the hash, longest to shortest. Then I search and replace on the string. Everywhere I find something from the hash, I replace it with the character whose code is in the hash. The /e flag says the second portion of the s/// is Perl code that generates the replacement string.

  • Comment on RE: Regexp and transliteration between languages