簡體   English   中英

RegEx代碼在理論上有效,但在運行代碼時無效

[英]RegEx code works in theory but not when code is run

我正在嘗試使用此RegEx搜索:Ruby中的<div class="ms3">(\\n.*?)+< ,但是,一旦我到達最后一個字符“ <”,它就會完全停止工作。 我已經在Rubular中對其進行了測試,而RegEx可以正常工作,我使用rubymine編寫了我的代碼,但是我也使用Powershell對它進行了測試,結果也相同。 否錯誤消息。 當我運行<div class="ms3">(\\n.*?)+它會打印<div class="ms3">這正是我要查找的內容,但是只要我添加了“ <”一無所獲。

我的代碼:

#!/usr/bin/ruby
# encoding: utf-8

File.open('ms3.txt', 'w') do |fo|
  fo.puts File.foreach('input.txt').grep(/<div class="ms3">(\n.*?)+/)
end

我正在搜索的一些內容:

  <div class="ms3">
    <span xml:lang="zxx"><span xml:lang="zxx">Still the tone of the remainder of the chapter is bleak. The</span> <span class="See_In_Glossary" xml:lang="zxx">DAY OF THE <span class="Name_Of_God" xml:lang="zxx">LORD</span></span> <span xml:lang="zxx">holds no hope for deliverance (5.16–18); the futility of offering sacrifices unmatched by common justice is once more underlined, and exile seems certain (5.21–27).</span></span>
  </div>

  <div class="Paragraph">
    <span class="Verse_Number" id="idAMO_5_1" xml:lang="zxx">1</span><span class="scrText">Listen, people of Israel, to this funeral song which I sing over you:</span>
  </div>

  <div class="Stanza_Break"></div>

我需要做的完整RegEx是<div class="ms3">(\\n.*?)+<\\/div>它拾取了第一部分,沒有其他內容

您的問題始於使用File.foreach('input.txt') ,它將結果分成幾行。 這意味着該模式分別與每行匹配,因此沒有一行與該模式匹配(根據定義,沒有一行中間有\\n )。

您應該更好地將整個文本作為一個塊閱讀並在其上使用match

File.read('input.txt').match(/<div class="ms3">(\n.*?)+<\/div>/)
# => #<MatchData "<div class=\"ms3\">\n    <span xml:lang=\"zxx\">
# => <span xml:lang=\"zxx\">Still the tone of the remainder of the chapter is bleak. The</span> 
# => <span class=\"See_In_Glossary\" xml:lang=\"zxx\">DAY OF THE 
# => <span class=\"Name_Of_God\" xml:lang=\"zxx\">LORD</span></span> 
# => <span xml:lang=\"zxx\">holds no hope for deliverance (5.16–18); 
# => the futility of offering sacrifices unmatched by common justice is once more 
# => underlined, and exile seems certain (5.21–27).</span></span>\n  </div>" 1:"\n  ">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM