2012-01-02 7 views
0

私は、テキストファイルがあります。ファイル内のコンテンツを検索して一致させるにはどうすればよいですか?

<table style="background-color: #f3f3f3; font-family: Arial; font-size: 8pt; border-top: #e7e7e7 5px solid" border="0" cellspacing="0" cellpadding="0"> 
    <tbody> 
<tr> 
<td style="padding-bottom: 20px; padding-left: 20px; padding-right: 20px; padding-top: 20px"> 
<p style="color: #b0b0b0"><font color="#808080" size="1"><strong>Important information</strong>: on this communication as it does not purport to be comprehensive. This disclaimer does not purport to exclude any warranties implied by law which may not be lawfully excluded. We have taken precautions to minimise the risk of transmitting software viruses, but we advise you to carry out your own virus checks on any attachment to this e-mail. We cannot accept liability for any loss or damage caused by software </p> 

これは、ウェブサイトのダンプではありませんが、それはアプリケーションがファイルに入れものです。テキストファイルをチェックするための

私の方法は、次のようになります。これが動作しない

def check_email_exists(firstname, email_sub, search_string) 
email_fldr="C:\\Agent\\TestMailFolder" 
email_id="[email protected]" 
Dir.chdir("#{email_fldr}\\#{firstname}") do 
    Dir.glob("#{email_id}*#{email_sub}*") do |filename| 
    File.open(filename) do |file| 
     file.readlines(filename).index("#{search_string}") 
    end 
    end 
    end 
end 

文字列であるsearch_stringに値を渡しています。たとえば、string = "transmitting software"がファイルに含まれているかどうかを確認しようとしています。また、ファイルには存在しないランダムな文字列が含まれているかどうかを確認しています。この場合、ファイル内の値を見つけて一致する場合は合格、失敗した場合は失敗します。

答えて

0

ファイルにはHTMLが含まれています。 HTMLを含む90%以上のアプリケーションでは、パーサーを使用する必要があります。私はNokogiriをお勧めします。

出力
require 'nokogiri' 

html = <<EOT 
<table style="background-color: #f3f3f3; font-family: Arial; font-size: 8pt; border-top: #e7e7e7 5px solid" border="0" cellspacing="0" cellpadding="0"> 
    <tbody> 
<tr> 
<td style="padding-bottom: 20px; padding-left: 20px; padding-right: 20px; padding-top: 20px"> 
<p style="color: #b0b0b0"><font color="#808080" size="1"><strong>Important information</strong>: on this communication as it does not purport to be comprehensive. This disclaimer does not purport to exclude any warranties implied by law which may not be lawfully excluded. We have taken precautions to minimise the risk of transmitting software viruses, but we advise you to carry out your own virus checks on any attachment to this e-mail. We cannot accept liability for any loss or damage caused by software </p> 
EOT 

doc = Nokogiri::HTML::DocumentFragment.parse(html) 

content = doc.content 

puts content 

Important information: on this communication as it does not purport to be comprehensive. This disclaimer does not purport to exclude any warranties implied by law which may not be lawfully excluded. We have taken precautions to minimise the risk of transmitting software viruses, but we advise you to carry out your own virus checks on any attachment to this e-mail. We cannot accept liability for any loss or damage caused by software 

を使用すると、結果が文字列が含まれている場合だけでなく、これを試してください「ソフトウェアを送信する」を参照したい場合:あなたの応答のための

puts "contains tranmitting software" if (content['transmitting software']) 
+0

おかげで、私がやりましたnokogiriを使うという考えを得る。 – user1126946

+0

私は以前、これらのシナリオをテストするためにキュウリを使用していると言及していませんでした – user1126946

関連する問題