我试图在ruby中的两个字符串之间获取一组文本,我似乎无法获得正确的方法或使用正确的正则表达式。
文本:
<html> <body> <!-- begin posts --> <h1>all kinds of html<h1> <p> blah blah </p> <p> i've been working on this forever </p> <!-- end posts --> </html> </body>我只想从<!-- begin posts -->到<!-- end posts --> (包含),并将该文本块保存在文本文件中。
我想出了如何在开始时打印该行:
File.open("index.html").each_line do |line| body.each {|line| puts line if line =~ /<!-- begin/}
但不是直到最后一个字符串之后的行。
我有一个红宝石在这里http://rubular.com/r/0W9QDpMGkM ,我一直无法找出任何东西。
感谢大家提前。
i'm trying to get a group of text in between 2 strings in ruby, and i can't seem to get the right method or use the right regex.
text:
<html> <body> <!-- begin posts --> <h1>all kinds of html<h1> <p> blah blah </p> <p> i've been working on this forever </p> <!-- end posts --> </html> </body>and i just want to get everything from <!-- begin posts --> to <!-- end posts -->, inclusive, and save that block of text in a text file.
i figured out how to print the line in the beginning:
File.open("index.html").each_line do |line| body.each {|line| puts line if line =~ /<!-- begin/}
but not the lines after up and until the last string.
i have a rubular here http://rubular.com/r/0W9QDpMGkM where i haven't been able to figure out anything.
thanks everyone in advance.
最满意答案
不要一行一行地做,只是将整个东西啜饮成一个字符串并撕开它:
s = File.read('index.html') want = s.match(/<!-- begin posts -->(.*)<!-- end posts -->/m)[1]现在你的标记之间的一切都是want 。 不要忘记正则表达式中的m修饰符。
当你修改你的输入时,你也可以删除流浪的前导和尾随空格:
want = s.match(/<!-- begin posts -->(.*)<!-- end posts -->/m)[1].strip正如下面的Tudor注意到的,如果您认为有多个<!-- end posts -->标记的机会,您可能希望对该组使用非贪婪(.*?) ; 当他们真的要你的时候,不要因为有点偏执而受伤。
参考文献:
File.read (实际上是IO.read ) String#match String#strip更新 :字符串的match方法返回MatchData对象。 数组访问运算符 :
... mtch[0]相当于特殊变量$& ,并返回整个匹配的字符串。 mtch[1] , mtch[2]等等返回匹配反向引用的值(圆括号中的模式部分)。
用于访问匹配的部分。 正则表达式中只有一个组,因此[1]可以在没有周围的HTML注释分隔符的情况下为您提供该组的内容。
Don't do it line by line, just slurp the whole thing into a string and rip it apart:
s = File.read('index.html') want = s.match(/<!-- begin posts -->(.*)<!-- end posts -->/m)[1]And now everything between your markers is in want. Don't forget the m modifier on the regex.
While you're mangling your input you can strip out the stray leading and trailing whitespace too:
want = s.match(/<!-- begin posts -->(.*)<!-- end posts -->/m)[1].stripAs Tudor notes below, you might want to use a non-greedy (.*?) for the group if you think there is any chance of multiple <!-- end posts --> markers; doesn't hurt to be a little paranoid when they really are you to get you.
References:
File.read (actually IO.read) String#match String#stripUPDATE: the match method on a string returns a MatchData object. The array access operator:
... mtch[0] is equivalent to the special variable $&, and returns the entire matched string. mtch[1], mtch[2], and so on return the values of the matched backreferences (portions of the pattern between parentheses).
Is used to access the matching parts. There's only one group in the regex so [1] gets you the contents of that group without the surrounding HTML comment delimiters.
更多推荐
发布评论