Mysql 处理SQL文件中每一行的Ruby脚本

Mysql 处理SQL文件中每一行的Ruby脚本,mysql,ruby,regex,gsub,Mysql,Ruby,Regex,Gsub,我写的这个脚本有一个问题,它是为了在.sql文件中搜索并替换某些字符串内容。例如 我试图取代: result of using this information. If you have any comments, queries or concerns with regards to the above information, Please <a href="#" target="_blank">Click Here</a>&nbsp;for differ

我写的这个脚本有一个问题,它是为了在.sql文件中搜索并替换某些字符串内容。例如

我试图取代:

result of using this information. If you have any comments, queries or concerns with regards to the above information, 

Please <a href="#" target="_blank">Click Here</a>&nbsp;for different contact options.</p>
<h4>Stone properties:</h4>
<p><span>Scientific name of the stone:</span> Quartz/Silicon dioxide</p>
<p><span>Group:</span> Silicates &ndash; tektosilicates</p>
还有许多其他的变化,但都没有多大的成功。我该如何做才能让它用新的字符串替换整个匹配的段落,而不仅仅是单个匹配的行?我也有一些感觉,我在Ruby字符串和类上犯了一些错误


我知道这是Ruby Regex 101,但我似乎无法理解。非常感谢。

您拨打的每一条电话都是
,因此您一次只能接到一条电话。有鉴于此,我相信你很清楚为什么会看到你看到的结果

因为只有1000个这样的部分,所以您可以读取整个文件,并使用捕获组进行全局替换,以获得所需的结果

我无法让正则表达式在支持替换的regexplanet上工作,但您可以看到匹配组在工作。完成匹配后,可以使用结合匹配组引用(\1、\2、\3、\4)的文字来构造新的替换文本,如中所示:

\1
    \2
  • \三,
  • \四,

我想了解一些有关捕获组的其他信息。我做了初步的研究,并尝试过,但没有成功。但有一个问题是,“each_line”代码是在每个.sql行上迭代还是在文件中每行的html“\r\n”上迭代?因为我从输出中注意到的问题是,“#{new_string}”替换行中“/*?*?*?*?/o”的每个实例,而不是作为该行的整个组?当我尝试使用'm=s.gsub(#{xyz},“#{new_string}”)来修复此问题时,它不起作用?它会迭代文件的每一行,Rocky。至于捕获组,我将启动一个正则表达式提琴,您可以查看。有关带有捕获组的正则表达式的参考,请参阅更新的答案。
Please <a href="#" target="_blank">Click Here</a>&nbsp;for different contact options.</p>
<ul class="navlistjdxcms">
<h4>Stone properties:</h4>
<li><span>Scientific name of the stone:</span> Quartz/Silicon dioxide</li>
<li><span>Group:</span> Silicates &ndash; tektosilicates</li>
full_path_to_read = File.expand_path('C:\Users\huber\Desktop\RubyReg\cms_page.sql')
full_path_to_write = File.expand_path('C:\Users\huber\Desktop\RubyReg\cms_page2.sql')

stringla = ""

File.open(full_path_to_read).each_line { |s|

    contents = s
    xyz = contents.scan(/<p><span>.*?<\/span>.*?<\/p>/o)
    new_str = xyz.to_s.gsub('<p>', '<li>')
    new_str2 = new_str.gsub('</p>', '</li>')
    new_string = '<ul class="navlistjdxcms">' + new_str2 + '</ul>'
    m = s.gsub((/<p><span>.*?<\/span>.*?<\/p>/o), "#{new_string}")
    stringla += m
}

File.open(full_path_to_write, "w+") { |f| f.write(stringla) }
<ul class="navlistjdxcms"> 
/<p><span>.*?<\/span>.*?<\/p>/o 
m = s.gsub("#{xyz}", "#{new_string}")
\1
<ul class="navlistjdxcms">
\2
<li>\3</li>
<li>\4</li>