small problem with easy regex...I have an input and need the text between 2 words. Example of input:
Blah Blah
Word1
New line text I need
Another important sentence for me
Word2
Blah blah
Word1
Line of important text
Word2
The end
And I need all the text between Word1 and Word2..Any tips?
You can use look-ahead and look-behind features of regular expressions:
str = <<HERE
Blah Blah
Word1
New line text I need
Another important sentence for me
Word2
Blah blah
Word1
Line of important text
Word2
The end
HERE
str.scan(/(?<=Word1).+?(?=Word2)/m) # => ["\nNew line text I need\nAnother important sentence for me\n", "\nLine of important text\n"]
Assuming the text is fed as keyboard input
while gets()
@found=true if line =~ /Word1/
next unless @found
puts line
@found=false if line =~ /Word2/
end
will print all lines between Word1 and Word2 inclusive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With