// http://stackoverflow.com/q/33707330/5290909 using System; using System.Text.RegularExpressions; public class Test { public static void Main() { string strIn = @""; string pattern = @" < (?(a)|img)\b [^>]*? \b(?(?(2)href|src)) \s*=\s* (?:""(?(?>\\.|[^\\""_#?&]+)*(?:_|(?[#?&]))(?>\\.|[^""\\]+)*)"" | '(?(?>\\.|[^\\'_#?&]+)*(?:_|(?[#?&]))(?>\\.|[^'\\]+)*)') "; Regex re = new Regex( pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace); MatchCollection underscoreLinks = re.Matches(strIn); for (int mnum = 0; mnum < underscoreLinks.Count; mnum++) { //loop matches Match match = underscoreLinks[mnum]; Console.WriteLine("Match #{0} - Value: {1}", mnum + 1, match.Value); } } }