re = /(?><a.*?>(.*?)<\/a>)\s*<ul/
str = '<ul>
<li>
<a href="#o">Первый</a>
</li>
<li>
<a href="#v">Второй</a>
</li>
<li>
<a href="#t">Третий</a>
</li>
<li>
<a href="#ch">Четвертый</a>
</li>
<li>
<a href="#p">ПÑтый</a>
</li>
<li>
<a href="#test">ШеÑтой</a>
<ul>
<li>
<a href="#so">ШеÑтьОдин</a>
</li>
<li>
<a href="#sd">ШеÑтьДва</a>
</li>
<li>
<a href="#st">ШеÑтьТри</a>
</li>
<li>
<a href="#sc">ШеÑтьЧетыре</a>
</li>
<li>
<a href="#sp">ШеÑтьПÑть</a>
</li>
<li>
<a href="#ss">ШеÑтьШеÑть</a>
</li>
<li>
<a href="#sss">ШеÑтьСемь</a>
</li>
<li>
<a href="#sv">ШеÑтьВоÑемь</a>
</li>
<li>
<a href="#sd">ШеÑтьДевÑть</a>
</li>
</ul>
</li>
</ul>'
# Print the match result
str.match(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