$re = '/<a[^>]*?href=(?|"([^"]*?)"|\'([^\']*?)\').*?>(.*?)<\/a>/si';
$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>
';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
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 PHP, please visit: http://php.net/manual/en/ref.pcre.php