开发者

Perl how to strip nested bbcode tags?

Here is the code:

use perl5i::2;

my $string = '[size 9]Some larger text. [i]Italic[/i] here.[/size]And [b]bold[/b] text.';
$string =~ s/\[(.+).*?\](.+)\[\/\1\]/$2/gi;

$string->say; 

The result is here:

Some larger text. [i]Italic[/i] here.And bold text.

Is there a single regex to strip the t开发者_如何学运维ags?


If all you want to do is strip the tags, you don't really need to ensure that the tags match: just remove anything inside square brackets.

If checking for nesting really is important, you can simply apply your current substitution repeatedly.


Do you want to strip all tags? Elements can be nested, but tags can't be nested, so there's nothing to it really.

s/\[[^\[\]]*\]//g;


What about Parse::BBCode?

Update:

You don't need to output HTML with this module. Try the following instead:

#!/usr/bin/perl

use strict;
use warnings;

use Parse::BBCode;

my %tags = map { $_ => '%s' } qw(
    b i u color size font highlight left right center indent email url thread post
    list img video code php html quote noparse attach bug PGN2 PGN3 threadvb wiki
);
my $parser = Parse::BBCode->new ( { tags => \%tags } );

my $string = '[size="9"]Some larger text. [i]Italic[/i] here.[/size]And [b]bold[/b] text.';
my $rendered = $parser->render( $string );

print "$rendered\n";

This way you don't have to parse any text yourself, which is a Good Thing™.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜