re = /<section.*id="section[\d]*"[\s\S]*?<\/section>/m
str = '<!DOCTYPE html>
<html lang="en">
<head>
<title>Whatever</title>
</head>
<body>
<section class="all-classes" id="section1-do-not-remove-section">
content2
content
</section>
<section class="all-classes" id="section2">
content
content2
</section>
</body>
<section class="all-classes" id="section3">
content
<div class="all-classes" id="section4">
anything
whatever
</div>
</section>
<section class="all-classes" id="section5-do-not-remove-section">
content
</section>
<section class="all-classes" id="section6">
content
</section>
<section class="all-classes" id="section7">
content
</section>
'
# 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