Here Perl allows us with the missing constructions of the \< and \> constructs (beginning and end of a word) that are available elsewhere. A pattern such as \b...\b will match even if ... doesnβt consume any of the target string, because the second \b will happily match the word boundary for the second time.
However, the boundary of the final word is simple (?<=\w)(?!\w) , so we can use this instead.
This program will do what you want. He looks for perspective for a string of potential Roman characters enclosed in word boundaries (so that we should be on the border of the original word), and then checks the legal Roman number, which is not followed by the word character (so now we, re at the boundary of the final word) .
Please note that I changed your labels >...< as they confuse me.
use strict; use warnings; use feature 'say'; my $x = ' some text I-LXIII iv more '; if ( $x =~ s{ (?= \b [CLXVI]+ \b ) ( (?:XC|XL|L?X{0,3})? (?:IX|IV|V?I{0,3})? ) (?!\w) } {<$1>}xgi ) { say $x; }
Exit
some text <I>-<LXIII> <iv> more
source share