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.


In reply to RE: Regexp and transliteration between languages by gnat
in thread Regexp and transliteration between languages by killedar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.