using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"<a[^>]*?href=(?|""([^""]*?)""|'([^']*?)').*?>(.*?)<\/a>";
string input = @"# 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>
";
RegexOptions options = RegexOptions.Singleline | RegexOptions.IgnoreCase;
foreach (Match m in Regex.Matches(input, pattern, options))
{
Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
}
}
}
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 C#, please visit: https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex(v=vs.110).aspx