Characters must match both the class AND the position. I want to ignore characters that are "out of order" (the "I" in "IV") but allow subsequent matches of characters that are in order (the "V" in "IV"). I want to make sure a period (".") replaces any character that doesn't match both the character and its designated position.

As far as I can tell your desired output does not match the above description. The expected output for "XIV" is "....XV." and the expected output for "XIX" is "....X.I" I dont see how these two interpretations are compatible with each other. A few other cases are also unclear, notably the one that says "CDXLVI" should result in ".D..XVI", which I can't understand at all. Anyway, I wrote two solutions, one parsing from the front and one from the back, neither gets all your output cases correct, which makes me suspect your output expectations are incorrect.

use strict; use warnings; my $max=0; my %val=map { $_ => $max++ } split //,'MDCLXVI'; sub rtol { my ($word)=@_; my @r = (".") x $max; my $last = $max + 1; while (length $word) { my $c=chop $word; if ($val{$c}<$last) { $r[$val{$c}]=$c; $last=$val{$c}; } } return join "",@r; } sub ltor { my ($word)=@_; my @r = (".") x $max; my $last = -1; for my $c (split //,$word) { if ($val{$c}>$last) { $r[$val{$c}]=$c; $last=$val{$c}; } } return join "",@r; } printf "%7s => %7s ( %7s : %-3s) ( %7s : %-3s )\n", qw(Input Expect L +toR ok? RroL ok?); printf "%7s %7s %7s %-3s %7s %-3s \n", ("---") x 6; while(<DATA>) { last unless /\S/; chomp; my ($input,$expect)=split /\s+/,$_; my $ltor=ltor($input); my $rtol=rtol($input); printf "%7s => %7s ( %7s : %-3s) ( %7s : %-3s )\n", $input, $expect, $ltor, $ltor eq $expect ? 'Ok' : 'Not', $rtol, $rtol eq $expect ? 'Ok' : 'Not'; } __END__ I ......I IV .....V. V .....V. VI .....VI IX ....X.. X ....X.. XI ....X.I XIV ....XV. XV ....XV. XVI ....XVI XIX ....X.I X ....X.. XL ....X.. LX ...LX.. XC ....X.. CLXIX ..CLX.I CDXLVI .D..XVI MCMXCVI M.C.XVI MDCLI MDCL..I
Input => Expect ( LtoR : ok?) ( RroL : ok? ) --- --- --- --- --- --- I => ......I ( ......I : Ok ) ( ......I : Ok ) IV => .....V. ( ......I : Not) ( .....V. : Ok ) V => .....V. ( .....V. : Ok ) ( .....V. : Ok ) VI => .....VI ( .....VI : Ok ) ( .....VI : Ok ) IX => ....X.. ( ......I : Not) ( ....X.. : Ok ) X => ....X.. ( ....X.. : Ok ) ( ....X.. : Ok ) XI => ....X.I ( ....X.I : Ok ) ( ....X.I : Ok ) XIV => ....XV. ( ....X.I : Not) ( ....XV. : Ok ) XV => ....XV. ( ....XV. : Ok ) ( ....XV. : Ok ) XVI => ....XVI ( ....XVI : Ok ) ( ....XVI : Ok ) XIX => ....X.I ( ....X.I : Ok ) ( ....X.. : Not ) X => ....X.. ( ....X.. : Ok ) ( ....X.. : Ok ) XL => ....X.. ( ....X.. : Ok ) ( ...L... : Not ) LX => ...LX.. ( ...LX.. : Ok ) ( ...LX.. : Ok ) XC => ....X.. ( ....X.. : Ok ) ( ..C.... : Not ) CLXIX => ..CLX.I ( ..CLX.I : Ok ) ( ..CLX.. : Not ) CDXLVI => .D..XVI ( ..C.XVI : Not) ( .D.L.VI : Not ) MCMXCVI => M.C.XVI ( M.C.XVI : Ok ) ( M.C..VI : Not ) MDCLI => MDCL..I ( MDCL..I : Ok ) ( MDCL..I : Ok )

Id be interested to see a better explanation of what you expect.

---
$world=~s/war/peace/g


In reply to Re: Help with a Regex by demerphq
in thread Help with a Regex by planetscape

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.