We Recommend

Accelerated C# 2008 Accelerated C# 2008
This book is both a rapid tutorial and a permanent reference. You’ll quickly master C# syntax while learning how the CLR simplifies many programming tasks. You’ll also learn best practices that ensure your code will be efficient, reusable, and robust. Why spend months or years discovering the best ways to design and code C# when this book will show you how to do things the right way, right from the start?


Posted By

RichardIII on 02/11/08


Tagged

match regular expressions collection


Versions (?)


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.