views:

102

answers:

4
str = "some html code [img]......[/img] some html code [img]......[/img]"

I want use regex get the array:

["[img]......[/img]","[img]......[/img]"]

Any ideas anyone?

+2  A: 

There is a ruby BBCODE parser at Google Code: http://code.google.com/p/ruby-bbcode/

Don't use regex for this.

Tomalak
i want get a array, not bbcode to html or html to bbcode
squarezw
@square:: Hm, my reading is that with a parser you can create any output you like, be it HTML or a simple array. This parser is a suggestion only, there are others, I'm sure. Key point is: Your time is better spent figuring out how to use a parser for this than trying to do it with regex. Even if it seems the other way around at first.
Tomalak
+2  A: 
irb(main):001:0> str = "some html code [img]......[/img] some html \
code [img]......[/img]"
"some html code [img]......[/img] some html code [img]......[/img]"
irb(main):002:0> str.scan(/\[img\].*?\[\/img\]/)
["[img]......[/img]", "[img]......[/img]"]

Keep in mind that this is a very specific answer that is based on your exact question. Change str by, say, adding an image tag within an image tag, and all Hell will break loose.

Yaser Sulaiman
A: 
str = "some html code [img]......[/img] some html code [img]......[/img]"
p str.split("[/img]").each{|x|x.sub!(/.*\[img\]/,"")}
+2  A: 

Please don't use BBCode. It's evil.

BBCode came to life when developers were too lazy to parse HTML correctly and decided to invent their own markup language. As with all products of laziness, the result is completely inconsistent, unstandardized, and widely adopted.

Try to use a user-friendlier markup language, like Markdown (what StackOverflow uses) or Textile.
Both of them have parsers for Ruby:

NullUserException
+1 just for the quote. although it's a parser, not an interpreter.
sreservoir
@sre I knew I couldn't be the *only* one who hated BBCode with a passion.
NullUserException
bbcode is an ill-conceived, badly-designed, and generally poorly-implemented html knock-off. its sole redeeming quality is that it tends to be shorter than html. and of course, that's not hard to do.
sreservoir
+1 BBCode is actually HTML with square brackets and synonyms.
BoltClock