Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何爆炸<;br>&书信电报;br/>&书信电报;br/>;字符串中的标签?_Ruby_Nokogiri - Fatal编程技术网

Ruby 如何爆炸<;br>&书信电报;br/>&书信电报;br/>;字符串中的标签?

Ruby 如何爆炸<;br>&书信电报;br/>&书信电报;br/>;字符串中的标签?,ruby,nokogiri,Ruby,Nokogiri,我有一个字符串,上面有一堆断开的标签 不幸的是,它们是不规则的 等 我正在使用nokogiri,但我不知道如何告诉它在每个断开标记处断开字符串 谢谢。如果您可以中断正则表达式,请使用以下分隔符: <\s*[Bb][Rr]\s*\/*> 说明: 一个左尖括号,零个或多个空格,B或B,R或R,零个或多个空格,零个或多个前斜杠 要使用正则表达式,请查看此处: 如果可以中断正则表达式,请使用以下分隔符: <\s*[Bb][Rr]\s*\/*> 说明: 一个左尖括号,零

我有一个字符串,上面有一堆断开的标签

不幸的是,它们是不规则的



我正在使用nokogiri,但我不知道如何告诉它在每个断开标记处断开字符串


谢谢。

如果您可以中断正则表达式,请使用以下分隔符:

<\s*[Bb][Rr]\s*\/*>

说明:

一个左尖括号,零个或多个空格,B或B,R或R,零个或多个空格,零个或多个前斜杠

要使用正则表达式,请查看此处:

如果可以中断正则表达式,请使用以下分隔符:

<\s*[Bb][Rr]\s*\/*>

说明:

一个左尖括号,零个或多个空格,B或B,R或R,零个或多个空格,零个或多个前斜杠

要使用正则表达式,请查看此处:

如果使用Nokogiri解析字符串,则可以扫描字符串并忽略除文本元素以外的任何内容:

require 'nokogiri'
doc = Nokogiri::HTML.parse('a<Br>b<BR>c<br/>d<BR/>e<br />f')
text = []
doc.search('p').first.children.each do |node|
  text << node.content if node.text?
end
p text  # => ["a", "b", "c", "d", "e", "f"]
需要“nokogiri”
doc=Nokogiri::HTML.parse('a
b
c
d
e
f') text=[] doc.search('p').first.children.each do |节点| 案文[“a”、“b”、“c”、“d”、“e”、“f”]

请注意,您必须搜索第一个p标记,因为Nokogiri会将整个内容包装在您的文本中,如果您使用Nokogiri解析字符串,则可以扫描它并忽略除文本元素以外的任何内容:

require 'nokogiri'
doc = Nokogiri::HTML.parse('a<Br>b<BR>c<br/>d<BR/>e<br />f')
text = []
doc.search('p').first.children.each do |node|
  text << node.content if node.text?
end
p text  # => ["a", "b", "c", "d", "e", "f"]
需要“nokogiri”
doc=Nokogiri::HTML.parse('a
b
c
d
e
f') text=[] doc.search('p').first.children.each do |节点| 案文[“a”、“b”、“c”、“d”、“e”、“f”]

请注意,您必须搜索第一个p标记,因为Nokogiri会将整个内容包装在您的文本中,以便实现iftrue的响应:

a = 'a<Br>b<BR>c<br/>d<BR/>e<br />f'
a.split(/<\s*[Bb][Rr]\s*\/*>/)
=> ["a", "b", "c", "d", "e", "f"]
a='a
b
c
d
e
f' a、 拆分(/) =>[“a”、“b”、“c”、“d”、“e”、“f”]

…在HTML中断之间留下一个字符串位数组。

所以要实现iftrue的响应:

a = 'a<Br>b<BR>c<br/>d<BR/>e<br />f'
a.split(/<\s*[Bb][Rr]\s*\/*>/)
=> ["a", "b", "c", "d", "e", "f"]
a='a
b
c
d
e
f' a、 拆分(/) =>[“a”、“b”、“c”、“d”、“e”、“f”]

…在HTML分隔符之间留下了一个字符串位数组。

Pesto的99%的方式是这样的,但是Nokogiri支持创建一个文档片段,该片段不会在声明中包装文本:

 text = Nokogiri::HTML::DocumentFragment.parse('<Br>this<BR>is<br/>a<BR/>text<br />string').children.select {|n| n.text? and n.content } 
puts text
# >> this
# >> is
# >> a
# >> text
# >> string
text=Nokogiri::HTML::DocumentFragment.parse(“


一个
文本
字符串”).children.select{n | n.text?和n.content} 放置文本 #>>这个 #>>是 #>>a #>>文本 #>>字符串
Pesto的99%的方法是这样的,但是Nokogiri支持创建一个文档片段,该片段不会将文本包装在声明中:

 text = Nokogiri::HTML::DocumentFragment.parse('<Br>this<BR>is<br/>a<BR/>text<br />string').children.select {|n| n.text? and n.content } 
puts text
# >> this
# >> is
# >> a
# >> text
# >> string
text=Nokogiri::HTML::DocumentFragment.parse(“


一个
文本
字符串”).children.select{n | n.text?和n.content} 放置文本 #>>这个 #>>是 #>>a #>>文本 #>>字符串
如何打破它?我使用gsub吗?string.gsub()?看起来您需要的是“拆分”。如何将其拆分?我使用gsub吗?string.gsub()?看起来“split”是您所需要的。使用just//i稍微简单一点,而使用just//i稍微简单一点