Regular Expressions

using System;
using System.Text.RegularExpressions;

namespace regexpressions
{
class Program
{
static void Main(string[] args)
{
string content = @"xyz <a href="http://www.xyz-xyz.com" >http://www.xyz-xyz.com</a> abc <a href="http://www.abc.org" >http://www.abc.org</a> def <a href="https://www.def.net" >https://www.def.net</a> vwx <a href="ftp://fileserver.vwx.org/dot/net";" >ftp://fileserver.vwx.org/dot/net";</a>
string pattern = @"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());
}
}
}

source

Leave a Reply