Regular Expressions


/ Published in: C#
Save to your folder(s)

A short Demo showing the use of Regular Expressions in C#.


Copy this code and paste it in your HTML
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. namespace regexpressions
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. string content = @"xyz http://www.xyz-xyz.com abc http://www.abc.org def https://www.def.net vwx ftp://fileserver.vwx.org/dot/net";
  11. string pattern = @"\b\S{1,5}://\S+\.\S{2,}"; //not a good url-pattern - only for demo
  12.  
  13. //more matches - find the urls
  14. MatchCollection Matches = Regex.Matches(content, pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
  15.  
  16. foreach (Match match in Matches)
  17. {
  18. Console.WriteLine(match.ToString());
  19. }
  20.  
  21. //one match - find only one (the first) url
  22. Match oneMatch = Regex.Match(content, pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
  23.  
  24. Console.WriteLine(oneMatch.ToString());
  25. }
  26. }
  27. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.