|There's more than one way to do things|
My Final Solution [Was: Re: Help with a Regex]by planetscape (Chancellor)
|on May 15, 2005 at 06:52 UTC ( #457171=note: print w/replies, xml )||Need Help??|
First, to report back on which Monk's proposed solutions worked best with my de-dyslexified output.
Animator's code worked flawlessly with my revised desired output. I used it as inspiration in my final solution, below.
tlm, demerphq, and insaniac posted solutions that came very close indeed to what I needed, but were not quite right for the problem. kwaping's was right on the money for its sole test case... Nevertheless, I have learned and will continue to learn from what each posted.
I thank all for their contributions, whether such contained code or not.
Now, to the problem and its final solution...
Please remember, the Roman Numerals were a "dumbed down" version of the real data and regex, to make for shorter code and test cases. As I mentioned, some characters that occur in an earlier part of my 'real' regex could also occur later. Note the comments in the code below to find out how I accomplished this.
I decided to split my problem into two steps, one step per script (and the posted regex is still a simplified version, though closer to actual). I wanted first to discard an earlier, one or two character match in favor of a longer, later match. I did this with LongestMatch.pl, below, which as noted in the script is pretty much a verbatim solution from Jeffrey Friedl's book, Mastering Regular Expressions .
The second script below, Align.pl, borrows heavily from Animator's offering. It takes the longest match and "pads" it for "missing" characters.
Also keep in mind, I'm still pretty new at this. My code may well still be ugly. Thanks to PM and the wisdom of its kind denizens, that's getting better. Thanks to the Monks listed above, the code now does what I what it to, too. That's more than I had when I started. Thanks again!
(BTW, for anyone wondering, it's a Linguistics thing. Morphology.)
In Section Seekers of Perl Wisdom