2009-12-11 3 views
1

가장 빠른 방법은 무엇입니까?xhtml 문서에서 특정 단어를 찾는 가장 빠른 방법

"지침"이라는 단어가 포함되거나 포함되지 않을 수도있는 html 문서를 여러 줄의 지시어로 표시 할 수 있습니다. "지침"이라는 단어와 그 뒤에 오는 줄이 포함 된 페이지를 구문 분석하고 싶습니다.

+0

당신이 따라 라인의 상수 또는 변수 숫자가 단어 "지침"을 발견 할 경우 따라 뭔가? – Asaph

답변

0

이 방법이 가장 올바른 방법은 아니지만 주로 작동합니다. 정규 표현식을 사용하여 문자열을 찾으십시오. ruby regex

원하는 정규식은/instructions ([^ <] +) /와 같습니다. 여기서는 < 문자로 끝나는 것으로 가정합니다.

0
문서가 일치하는 경우 당신은 단지 테스트하여 시작할 수 있습니다

:

if open('docname.html').read =~ /Instructions/ 
    # Parse to remove the instructions. 
end 

그때 당신이 원하는 부분을 추출하는 Hpricot을 사용하는 것이 좋습니다 것 - 이것이 당신의 HTML이 어떻게 구성되어 있는지에 따라 다소 어려울 것이다 . 좀 더 구체적인 도움이 필요하면 구조에 대한 자세한 정보를 게시하십시오.

1

어쩌면이 라인

require 'rubygems' 
require 'nokogiri' 

def find_instructions doc 
    doc.xpath('//body//text()').each do |text| 
    instructions = text.content.select do |line| 
     # flip-flop matches all sections starting with 
     # "Instructions" and ending with an empty line 
     true if (line =~ /Instructions/)..(line =~ /^$/) 
    end 
    return instructions unless instructions.empty? 
    end 
    return [] 
end 

puts find_instructions(Nokogiri::HTML(DATA.read)) 


__END__ 
<html> 
<head> 
    <title>Instructions</title> 
</head> 
<body> 
lorem 
ipsum 
<p> 
lorem 
ipsum 
<p> 
lorem 
ipsum 
<p> 
Instructions 
- Browse stackoverflow 
- Answer questions 
- ??? 
- Profit 

More 
<p> 
lorem 
ipsum 
</body> 
</html> 
관련 문제