Return to Snippet

Revision: 5086
at February 11, 2008 08:05 by RichardIII


Updated Code
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());
        }
    }
}

Revision: 5085
at February 11, 2008 08:02 by RichardIII


Initial Code
using System;
using System.Text.RegularExpressions;

namespace regexpressions
{
    class Program
    {
        static void Main(string[] args)
        {
            //more matches - find the urls
            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

            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());
        }
    }
}

Initial URL


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

Initial Title
Regular Expressions

Initial Tags


Initial Language
C#