s/(\d+)([st|nd|rd|th])+/num2en_ordinal($1)/ge;

The  | ordered alternation metacharacter is not meta in a character class. The character class [st|nd|rd|th] is equivalent to [stndrh|] (update: because repeated characters have no special significance in a class). The quantified capture group ([st|nd|rd|th])+ matches any of the characters in the [stndrh|] class one or more times and captures the last such character matched. Try matching against  '2ds' '33hn' '123|s' and a few other such strings. You may want something like the true alternation (?:st|nd|rd|th) possibly supported by some look-around assertions.

c:\@Work\Perl\monks>perl -wMstrict -le "my $s = '1ds rule: 22hn, 3rd and 123|sdnfloor'; ;; printf qq{'$&' } while $s =~ m{(\d+)([st|nd|rd|th])+}xmsg; " '1ds' '22hn' '3rd' '123|sdn'

Update: Maybe something like this:

c:\@Work\Perl\monks>perl -wMstrict -le "my $rx_ordinal_indicator = qr{ (?<! [[:alpha:]]) (?: st | nd | rd | th) (?! [[:alpha:]]) }xms; ;; my $s = '1ds rule: 22hn, 2 then 3rd and 44 th, 2 nd of the 123|sdnfl +oor'; ;; printf qq{'$&' } while $s =~ m{ (\d+) \s* $rx_ordinal_indicator }xms +g; " '3rd' '44 th' '2 nd'
(Ordinarily, I wouldn't use  $& and friends, but it's convenient for this example.)


Give a man a fish:  <%-{-{-{-<


In reply to Re^2: replace all numerics to words in a txt file -- oneliner by AnomalousMonk
in thread replace all numerics to words in a txt file by imhacked

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.