7

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.

rick
  • 1,075
  • 4
  • 20
  • 29
  • FYI, a better option for your use case may be to use ERB, see the examples in this answer: http://stackoverflow.com/questions/980547/how-do-i-execute-ruby-template-files-erb-without-a-web-server-from-command-line – Abdullah Jibaly Jul 09 '11 at 04:50
  • that looks really great. i'm going to try to explore it a little. thank you. – rick Jul 09 '11 at 05:02

3 Answers3

8

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:

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.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
3

try with:

printing = false
File.open("index.html").each_line do |line|
  printing = true if line =~ /<!-- begin/      
  puts line if printing
  printing = false if line =~ /<!-- end posts/
end
Tudor Constantin
  • 26,330
  • 7
  • 49
  • 72
  • this is giving me `syntax error, unexpected $end, expecting kEND` – rick Jul 09 '11 at 04:59
  • You could use the flip flop operator here. Then again, it [isn't commonly used](https://github.com/rubinius/rubinius/issues/1006), so the next person reading it may get confused. – Andrew Grimm Jul 09 '11 at 13:56
0
File.readlines(ARGV[0]).each do|line|
  if line =~ /<!-- begin posts -->/ .. line =~ /<!-- end posts -->/     
    puts line
  end
end
Sanjeev
  • 298
  • 4
  • 11