ruby在2个字符串之间打印选定的文本行(ruby print selected lines of text in between 2 strings)

编程入门 行业动态 更新时间:2024-10-08 00:27:19
ruby在2个字符串之间打印选定的文本行(ruby print selected lines of text in between 2 strings)

我试图在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].strip

As 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#strip

UPDATE: 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.

更多推荐

本文发布于:2023-08-03 23:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1402414.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   文本   print   ruby   text

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!