Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

ruby - Regex to split BBCode into pieces

I have this:

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

and I want to get this:

["[img]......[/img]","[img]......[/img]"]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 (that's what Stack Overflow uses) or Textile. Both of them have parsers for Ruby:


If you still don't want to heed to my advice and choose to go with BBCode, don't reinvent the wheel and use a BBCode parser. To answer your question directly, there is the least desirable option: use regex.

/[img].*?[/img]/

As seen on rubular. Although I would use /[img](.*?)[/img]/, so it will extract the contents inside the img tags. Note that this is fairly fragile and will break if there are nested img tags. Hence, the advice to use a parser.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...