in reply to Metatag processing (overlapping regions)

Nah, I'd go with your first approach:

#!/usr/bin/perl -w use strict; my $str= "aa<bb>aa<U>aa</bb>aa</U>aa"; $str =~ s#<bb>(.*?)</bb># my $x= $1; $x =~ s/a/bb/g; $x #ge; $str =~ s#<U>(.*?)</U>#\U$1#g; print "($str)\n" __END__ prints (aabbbbBBBBAAaa)
If you insist on processing the string once, then I'd build a separate output string so that you don't modify the input string.

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
RE: (tye)Re: Metatag processing (overlapping regions)
by kaatunut (Scribe) on Nov 12, 2000 at 02:44 UTC
    Yeah, so it might. But it isn't entirely nonproblematic approach, either:

    So, we have this processing function for <bb>, like, { s/a/bb/g; }. Cool. But what if I have another metatag called "<a>" ? It got instantly more complicated. Now I have to write code to the processor that will apply the substitution only for text outside of metatags, that means writing code to detect the tags inside the substitution text.

    Now, suppose I make another metatag "<foo>" that will substitute "foo => bar":

    "<foo>fo<U>o bar</U></foo>" => "baR BAR".

    Now, the replacer wouldn't find "foo" because there was "<U>" inside it, even though the "<U>" should be null length and invisible.

    Maybe that would explain my preference to do it with plaintext and array of offsets and tag types (the second approach)?

      And what happens if you want to define an escaped region?

      Or say that a \ escapes what would otherwise be a tag?

      My recommendation matches tye's. If the problem is going to stay simple then KISS, process each tag with a pass. If it isn't then create a one-pass algorithm where as you pass through the initial string you incrementally create the replaced string. This approach is more complex to set up, but once set up is a lot more flexible.

      One approach is to use Parse::RecDescent. There are lots of examples of that around, if you run into trouble then ask questions. Another is to roll your own, for an example of how to do that you could take a look at Why I like functional programming. (Skip to the function at the very end, the problem solved there is much more complex than yours here, and most of the code there is irrelevant to you.)

      But trying to do all of this logic with modifying in place is going to be simple insanity to figure out.