in reply to Help with a Regex
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.
|
|---|