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:
(Ordinarily, I wouldn't use $& and friends, but it's convenient for this example.)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'
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |