Ruby-Regex字符串提取
我正在尝试提取按以下方式格式化的字符串的内容:Ruby-Regex字符串提取,ruby,regex,Ruby,Regex,我正在尝试提取按以下方式格式化的字符串的内容: <script type="text/javascript"> document.viewData = THE INFORMATION I WANT </script> some other stuff document.viewData=我想要的信息 一些其他的东西 有没有关于如何实施的想法 提前谢谢 需要“nokogiri” require 'nok
<script type="text/javascript">
document.viewData = THE INFORMATION I WANT
</script> some other stuff
document.viewData=我想要的信息
一些其他的东西
有没有关于如何实施的想法
提前谢谢 需要“nokogiri”
require 'nokogiri'
doc = Nokogiri::XML::Document.parse <<-_XML_
<script type="text/javascript">
document.viewData = THE INFORMATION I WANT
</script> some other stuff
_XML_
doc.at('//script').text.strip.split("=").last
# => " THE INFORMATION I WANT"
doc=Nokogiri::XML::Document.parse根据您的严格程度,这可以完成工作(匹配组中的结果):
\W+document.viewData=\s+([^您的文本数据:
text = <<-_TEXT_
<script type="text/javascript">
document.viewData = THE INFORMATION I WANT
</script> some other stuff
_TEXT_
将其应用于文本并获得结果
result = (text.match re)[1]
print result
你的意思是哪一部分是内容?你是说字符串中嵌入了换行符,还是将这些换行符作为单独的字符串进行扫描?
re = /document\.viewData = (.*)/
result = (text.match re)[1]
print result