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.
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |