We help IT Professionals succeed at work.

regex remove nested tags

bobbit31
bobbit31 asked
on
i want to remove a tag as well as the text w/in that tag.

for example:

[quote]blah blah i don't want any of this stuff[/quote] and this is the rest

so i use:

/\[quote\](.*?)\[\/quote]/

which works fine until you have a nested quote like so:

[quote]hello world [quote]foo bar[/quote] blah blah[/quote] and this is the rest

your assistance is greatly appreciated.

Thanks!

Bob
Comment
Watch Question

ozo
SILVER EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
#!/usr/bin/perl
$_="[quote]hello world [quote]foo bar[/quote] blah blah[/quote] and this is the rest
[quote]blah blah i don't want any of this stuff[/quote] and this is the rest
"
;
($re=$_)=~s#((\[quote])|(\[/quote])|.)#${['(','']}[!$2]\Q$1\E${[')','']}[!$3]#gs;
$re=join"|",map{quotemeta}(eval{/$re/});
die $@ if $@;
s/$re//g;
print;

Author

Commented:
can you do that in ruby? please?
ozo
SILVER EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
Can you guarantee that there will only be one set of nested quotes to remove?
Or that they will be nested no more than two deep?.

Author

Commented:
i cannot :(

Author

Commented:
upping points
Commented:
/\[quote\].*\[/quote\]/m

I tested that here: http://www.rubular.com/

Author

Commented:
thanks all!

giving ozo some points too as his solution also works
ozo
SILVER EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
/\[quote\].*\[/quote\]/m
may not work properly without a guarantee that there will only be one set of quotes to remove
[quote]hello world [/quote]and this is the rest[quote] blah blah[/quote]

Explore More ContentExplore courses, solutions, and other research materials related to this topic.