Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.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-Regex字符串提取_Ruby_Regex - Fatal编程技术网

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