fork download
  1. // http://stackoverflow.com/q/33707330/5290909
  2. using System;
  3. using System.Text.RegularExpressions;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. string strIn = @"<a href=""someurl.html?query=foo"">";
  10. string pattern = @"
  11. <
  12. (?<Tag_Name>(a)|img)\b
  13. [^>]*?
  14. \b(?<URL_Type>(?(2)href|src))
  15. \s*=\s*
  16. (?:""(?<URL>(?>\\.|[^\\""_#?&]+)*(?:_|(?<Query>[#?&]))(?>\\.|[^""\\]+)*)""
  17. | '(?<URL>(?>\\.|[^\\'_#?&]+)*(?:_|(?<Query>[#?&]))(?>\\.|[^'\\]+)*)')
  18. ";
  19. Regex re = new Regex( pattern,
  20. RegexOptions.IgnoreCase | RegexOptions.Multiline
  21. | RegexOptions.IgnorePatternWhitespace);
  22.  
  23.  
  24. MatchCollection underscoreLinks = re.Matches(strIn);
  25. for (int mnum = 0; mnum < underscoreLinks.Count; mnum++)
  26. { //loop matches
  27. Match match = underscoreLinks[mnum];
  28. Console.WriteLine("Match #{0} - Value: {1}", mnum + 1, match.Value);
  29. }
  30. }
  31. }
Success #stdin #stdout 0.12s 24656KB
stdin
Standard input is empty
stdout
Match #1 - Value: <a href="someurl.html?query=foo"