/ Published in: C#

A short Demo showing the use of Regular Expressions in C#.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
using System; using System.Text.RegularExpressions; namespace regexpressions { class Program { static void Main(string[] args) { 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"; string pattern = @"\b\S{1,5}://\S+\.\S{2,}"; //not a good url-pattern - only for demo //more matches - find the urls MatchCollection Matches = Regex.Matches(content, pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture); foreach (Match match in Matches) { Console.WriteLine(match.ToString()); } //one match - find only one (the first) url Match oneMatch = Regex.Match(content, pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture); Console.WriteLine(oneMatch.ToString()); } } }
Comments
