[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 548
  • Last Modified:

regex remove nested tags

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
0
bobbit31
Asked:
bobbit31
  • 4
  • 3
2 Solutions
 
ozoCommented:
#!/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;
0
 
bobbit31Author Commented:
can you do that in ruby? please?
0
 
ozoCommented:
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?.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
bobbit31Author Commented:
i cannot :(
0
 
bobbit31Author Commented:
upping points
0
 
petiexCommented:
/\[quote\].*\[/quote\]/m

I tested that here: http://www.rubular.com/
0
 
bobbit31Author Commented:
thanks all!

giving ozo some points too as his solution also works
0
 
ozoCommented:
/\[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]
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now