re = /(?:^.+\n)*abc (\d+)|^(x)(?==)/m
str = 'x=0
abc 20
def 76
ghi 11
x=1
def 45
ghi 32
x=2
jkl 56
mno 76
abc 134
.
.
.
x=393
xyz 21
abc 9
x=394
ghi
def
xyz'
# Print the match result
str.scan(re) do |match|
puts match.to_s
end
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html