正则表达式将 BBCode 分成几部分

发布于 2024-09-24 16:05:38 字数 196 浏览 2 评论 0原文

我有这个:

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

我想得到这个:

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

I have this:

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

and I want to get this:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鹤仙姿 2024-10-01 16:05:38

请不要使用 BBCode。这是邪恶

BBCode 在开发人员的帮助下诞生了
懒得正确解析 HTML
并决定发明自己的标记
语言。与所有产品一样
偷懒,结果完全是
不一致、不标准化、
被广泛采用。

尝试使用用户友好的标记语言,例如 Markdown (这就是 Stack Overflow 使用的)或纺织品
它们都有 Ruby 解析器:


如果您仍然不想听从我的建议并选择使用 BBCode,请不要重新发明轮子并使用 BBCode 解析器。要直接回答您的问题,有最不理想的选择:使用正则表达式。

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

rubular 所示。虽然我会使用 /\[img\](.*?)\[\/img\]/ ,所以它会提取 img 标签内的内容。请注意,这是相当脆弱的,如果存在嵌套的 img 标签,则会损坏。因此,建议使用解析器。

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.

梦里°也失望 2024-10-01 16:05:38
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]"]

请记住,这是一个非常具体的答案,基于您的具体问题。更改 str,例如,在图像标签中添加图像标签,以及 一切地狱都会崩溃

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.

断肠人 2024-10-01 16:05:38

Google 代码中有一个 ruby BBCODE 解析器

不要为此使用正则表达式。

There is a ruby BBCODE parser at Google Code.

Don't use regex for this.

贵在坚持 2024-10-01 16:05:38
str = "some html code [img]......[/img] some html code [img]......[/img]"
p str.split("[/img]").each{|x|x.sub!(/.*\[img\]/,"")}
str = "some html code [img]......[/img] some html code [img]......[/img]"
p str.split("[/img]").each{|x|x.sub!(/.*\[img\]/,"")}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文