re = /<a[^>]*?href=(?|"([^"]*?)"|'([^']*?)').*?>(.*?)<\/a>/i
str = '# Basic Cases
<a href="http://test.com">double quotes</a>
<a href=\'http://test.com\'>single quotes</a>
<a class="blah a href" data="blah" href="#rel-link" rel=\'\'>complicated attributes</a>
<a href="#anchor-1">adjacent</a><a href="#anchor-2">anchors</a>
# Extra Cases
<a href="http://test.com">title
linebreak</a>
<a href="#anchor-reference"><span>inline span</span></a>
<ahref="uri:test-uri">technically invalid anchor</a>
<a href="http://test.com"><a href="#invalid">technically invalid internal anchor</a></a>
<a href="\'\'\'\'\'\\"\\"">quotes test</a>
# Random Junk Stress Tests
<a class="" href=""rergerger er erg><></><>.e.ewfe></wef/we. ></a>Test</a>
<ahref="e">e</a>
<a>e</a>
<a href="">e<a href="">e</a></a>
'
# 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