2011-03-22 43 views

回答

0

如果我正確理解你的問題,你想要做的是存儲的分隔符之前的謊言,在字符串中的HTML文件的一部分,例如:

<html> 
    <head> 
    <title>Blah</title> 
    </head> 
    <body> 
    <p>Some stuff</p> 
     <!-- Delimiter --!> 
    </body> 
</html> 

你想要的一切之前<!-- Delimiter --!>

在這種情況下,你也許可以做到這一點:

str = "" 
File.open("the_file.html","r"){|f|str << f.read} #If you need to read the html out of a file 
part_to_keep = str.split("<!-- Delimiter --!>").first 

讓我知道這是否是你需要的。

+0

你好,感謝您的快速回答。你已經正確理解我的要求。我會測試你的代碼,並讓你知道它是否工作。 – tranquiliste 2011-03-22 10:49:10

+0

這工作。謝謝 – tranquiliste 2011-03-22 18:17:08

+0

不客氣! :) – 2011-03-22 18:34:22

0

對於您可以使用一個Perl的一行如下Unix版本:

perl -n -e 'print if $delim; 
      $delim=1 if ($delim or /<!-- Delimeter --!>/);' html_file >output 

這是通過使用定點變量$ delim中檢測到的分隔符是否已經看到。分隔符後的所有行都將被打印。

+0

感謝Mauritz,我已經使用了Ruby代碼,但是我始終堅持您的解決方案。 – tranquiliste 2011-03-26 07:11:26