laziness, impatience, and hubris | |
PerlMonks |
Re: Bracketing Substring(s) in the Stringby fizbin (Chaplain) |
on Aug 26, 2005 at 16:07 UTC ( [id://486905]=note: print w/replies, xml ) | Need Help?? |
The code posted so far has relied on doing one substitution at a time, modifying substitutions so that they can handle internal brackets.
I'd like to propose a different approach, but it relies on one feature of the problem that you haven't made explicit: that all the strings you're dealing with, and all the substrings, are uppercase letters only. If so, then what we can do is make each match case-insensitive and lowercase any region that's found to match. Then, simply place brackets at uppercase/lowercase boundaries, and we're set:
And this passes all four of your test cases. But wait! Here's a test case that this code - and all the other code posted to this thread that I've tried - fails on: Oh dear, we didn't account for the case when the same substring overlaps itself. How are we going to handle this? Fortunately, it's not impossible. What we need to have happen is basically "repeat the substitution, but don't change anything already all in lowercase, until no changes are made". Fortunately, perl's got a syntax for that: That new line says:
In Section
Seekers of Perl Wisdom
|
|