2012-01-02 4 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

관련 문제