Please see

Script:

#! /usr/local/bin/perl -w use strict; my $final_string = ''; while (<>) { chomp; if (m/([mM]{0,1}) ([dD]{0,1}) ([cC]{0,1}) ([lL]{0,1}) ([xX]{0,1}) ([vV]{0,1}) ([iI]{0,1})\b/x) { if (length($1) == 0) { $final_string = "\."; } else { $final_string = $1; } if (length($2) == 0) { $final_string .= "\."; } else { $final_string .= $2; } if (length($3) == 0) { $final_string .= "\."; } else { $final_string .= $3; } if (length($4) == 0) { $final_string .= "\."; } else { $final_string .= $4; } if (length($5) == 0) { $final_string .= "\."; } else { $final_string .= $5; } if (length($6) == 0) { $final_string .= "\."; } else { $final_string .= $6; } if (length($7) == 0) { $final_string .= "\."; } else { $final_string .= $7; } print "$final_string\n"; $final_string = ''; } }

Input:

I IV V VI IX X XI XIV XV XVI XIX X XL LX XC CLXIX CDXLVI MCMXCVI MDCLI

Actual Output:

......I ....... .....V. .....VI ....... ....X.. ....X.I ....... ....XV. ....XVI ....... ....X.. ....... ...LX.. ....... ....... ....... ....... MDCL..I

Desired Output:

......I .....V. .....V. .....VI ....X.. ....X.. ....X.I ....XV. ....XV. ....XVI ....X.I ....X.. ....X.. ...LX.. ....X.. ..CLX.I .D..XVI M.C.XVI MDCL..I

The regex and the sample data have been somewhat contrived to fit Roman Numerals. The real regex/data is similar, but character classes may contain a variable number of characters. Certain classes later in the regex may contain some or all characters contained in prior classes.

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.

I am not sure how to change my regex to accomplish my goals. Any help would be appreciated.

for a complete description of my quandry, including my code, sample input, actual output, and an example of desired output.

I need help modifying my regex to get my desired output, but am not sure how to fix it.

Your wisdom is greatly appreciated.


Updated to use < r e a d m o r e > tags rather than my scratchpad, on the excellent advice of reasonablekeith.


In reply to 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.