tocie has asked for the wisdom of the Perl Monks concerning the following question:
my $string = q(
Outside.
{tag}
Inside level 1.
{tag}
Inside level 2.
{/tag}
Inside level 1.
{/tag}
Outside.
);
$string =~ s/\{tag\}(.+)\{\/tag\}/--Marked--\n$1\n--EndMarked--/gis;
Right, so that matches the first {tag} and the last {/tag}.
But nothing inbetween.
If I change the pattern to
/\{tag\}(.+?)\{\/tag\}/
it matches the first {tag} and the first {/tag}, but because the second {tag} was inside the block just matched, it's passed over when seeking the next {tag}.
while($string =~ s/\{tag\}(.+)\{\/tag\}/--Marked--\n$1\n--EndMarked--/gis) { next; }
That's a sub-optimal solution for the problem at hand, where there are dozens of these specialized markup tags.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Properly transforming strings with nested markup tags
by merlyn (Sage) on Dec 29, 2001 at 08:36 UTC | |
by tocie (Novice) on Dec 29, 2001 at 12:05 UTC | |
|
Re: Properly transforming strings with nested markup tags
by I0 (Priest) on Dec 29, 2001 at 10:09 UTC | |
by tocie (Novice) on Dec 29, 2001 at 12:11 UTC | |
|
Re: Properly transforming strings with nested markup tags
by khkramer (Scribe) on Dec 29, 2001 at 10:54 UTC | |
by tocie (Novice) on Dec 29, 2001 at 12:09 UTC | |
|
(crazyinsomniac) Re: Properly transforming strings with nested markup tags
by crazyinsomniac (Prior) on Dec 29, 2001 at 17:22 UTC |