我在包含HTML的MySQL列中有一个文本blob字段.我必须改变一些标记,所以我想我会用ruby脚本来做.Ruby在这里无关紧要,但是看到它的答案会很高兴.标记如下所示:
<h5>foo</h5>
<table>
<tbody>
</tbody>
</table>
<h5>bar</h5>
<table>
<tbody>
</tbody>
</table>
<h5>meow</h5>
<table>
<tbody>
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
我需要将<h5>foo</h5>每个文本的第一个块更改为仅<h2>something_else</h2>保留字符串的其余部分.
使用Ruby似乎无法获得正确的PCRE正则表达式.
Phr*_*ogz 31
# The regex literal syntax using %r{...} allows / in your regex without escaping
new_str = my_str.sub( %r{<h5>[^<]+</h5>}, '<h2>something_else</h2>' )
Run Code Online (Sandbox Code Playgroud)
使用String#sub而不是String#gsub导致仅发生第一次替换.如果你需要动态选择'foo'是什么,你可以在正则表达式文字中使用字符串插值:
new_str = my_str.sub( %r{<h5>#{searchstr}</h5>}, "<h2>#{replacestr}</h2>" )
Run Code Online (Sandbox Code Playgroud)
再说一遍,如果你知道'foo'是什么,你就不需要一个正则表达式:
new_str = my_str.sub( "<h5>searchstr</h5>", "<h2>#{replacestr}</h2>" )
Run Code Online (Sandbox Code Playgroud)
甚至:
my_str[ "<h5>searchstr</h5>" ] = "<h2>#{replacestr}</h2>"
Run Code Online (Sandbox Code Playgroud)
如果你需要运行代码来找出替换,你可以使用sub的块形式:
new_str = my_str.sub %r{<h5>([^<]+)</h5>} do |full_match|
# The expression returned from this block will be used as the replacement string
# $1 will be the matched content between the h5 tags.
"<h2>#{replacestr}</h2>"
end
Run Code Online (Sandbox Code Playgroud)
每当我必须解析或修改HTML或XML时,我都会找到解析器.我几乎从不打扰正则表达式或instring,除非它绝对是一个明智的选择.
以下是使用Nokogiri的方法,没有任何正则表达式:
text = <<EOT
<h5>foo</h5>
<table>
<tbody>
</tbody>
</table>
<h5>bar</h5>
<table>
<tbody>
</tbody>
</table>
<h5>meow</h5>
<table>
<tbody>
</tbody>
</table>
EOT
require 'nokogiri'
fragment = Nokogiri::HTML::DocumentFragment.parse(text)
print fragment.to_html
fragment.css('h5').select{ |n| n.text == 'foo' }.each do |n|
n.name = 'h2'
n.content = 'something_else'
end
print fragment.to_html
Run Code Online (Sandbox Code Playgroud)
解析后,这就是Nokogiri从片段中返回的内容:
# >> <h5>foo</h5>
# >> <table><tbody></tbody></table><h5>bar</h5>
# >> <table><tbody></tbody></table><h5>meow</h5>
# >> <table><tbody></tbody></table>
Run Code Online (Sandbox Code Playgroud)
这是在运行之后:
# >> <h2>something_else</h2>
# >> <table><tbody></tbody></table><h5>bar</h5>
# >> <table><tbody></tbody></table><h5>meow</h5>
# >> <table><tbody></tbody></table>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24417 次 |
| 最近记录: |