Posted By

RichardIII on 02/11/08


Tagged

match regular expressions collection


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

enajenkins
umang_nine
vali29
rodrigojuarez
franksmile0310
Priestd09


Regular Expressions


 / Published in: C#
 

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

  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  

You need to login to post a comment.