looking for a brokerage account or IRA... click here Add To Favorites
return to index 

C# Regular Expression Example

The example at the bottom will build a program with which to test regular expressions. I have included the compiled file at the bottom. It should be clear how to modify it to test your own expressions. I attempted to use basic code that would show the underlying structure. Always remember that Match.Groups[0].Value is the entire matching element and not a group indicated by parantheses.

RegexOptions

RegexOptionDescription
Noneno options specified
IgnoreCasecase insensitive pattern match
Multiline$ and ^ refer to the beginning and end of lines rather than the entire string being matched
ExplicitCaptureonly captures named items
SingleLine(.) captures as well, otherwise ignored
Compiledfaster run time for longer start time
IgnorePatternWhitespaceun escaped whitespace excluded from capture
RightToLeftmatches right to left











Case-Insensitive RegEx (Ignore Case)

To ignore case in Regex, you specify a Regex Option when creating a member of the class. The command is RegexOptions.IgnoreCase. It is part of a series of options that tell the system how to deal with multiple lines, case, etc.

Suppose I wanted to check for all words, comprised of the alphabet, in a line in Regex. I could use either of the following: Regex r = new Regex("[a-z]+", RegexOptions.IgnoreCase); or Regex r = new Regex("[A-Za-z]+");. Notice how i must specify the capital letters in one but not the other.

Use Multple Regex Options
Separate multiple regexoptions with a vertical bar "|". e.g. RegexOptions.IgnoreCase | RegexOptions.Multiline

Example Regular Expressions

Grab HTML - <([A-Z][A-Z0-9]*[^>]*>(.*?)

Trim Whitespace - ^[ ]+|[ ]+$

Email Address -


Click Here To Download the Compiled Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace examples
{
    class Program
    {
        static void Main(string[] args)
        {
            string regex = "", example = "";
            Regex r;
            MatchCollection mc;

            regex ="[0-9]+";
            r = new Regex(regex, RegexOptions.Multiline | RegexOptions.IgnoreCase);
            Console.WriteLine("Please enter a string with numbers randomly inserted: (abd3dne2ns)");
            Console.WriteLine("The string will be tested by the following Regex: {0}",regex);
            example = Console.ReadLine().Trim();

            //Test the string
            mc = r.Matches(example);
            foreach (Match m in mc)
            {
                Console.WriteLine("	Match: {0}", m.Value);
            }
            Console.WriteLine("");

            regex = "[0-9]*";
            r = new Regex(regex, RegexOptions.Multiline | RegexOptions.IgnoreCase);
            Console.WriteLine("Please enter a string with numbers randomly inserted: (abd3dne2ns)");
            Console.WriteLine("The string will be tested by the following Regex: {0}", regex);
            example = Console.ReadLine().Trim();

            //Test the string
            mc = r.Matches(example);
            foreach (Match m in mc)
            {
                Console.WriteLine("	Match: {0}", m.Value);
            }

            Console.WriteLine("Do you notice the difference between the * and +");
            Console.WriteLine("");
            regex = "<([a-z0-9]+)[^>]*>";
            r = new Regex(regex, RegexOptions.Multiline | RegexOptions.IgnoreCase);
            Console.WriteLine("Please enter an HTML tag and I will find the tag type: ()");
            Console.WriteLine("The string will be tested by the following Regex: {0}", regex);
            example = Console.ReadLine().Trim();

            //Test the string
            mc = r.Matches(example);
            foreach (Match m in mc)
            {
                Console.WriteLine("	Match: {0}", m.Groups[1].Value);
            }


        }
    }
}

Additional Interesting Articles

SQL Create Procedure
PHP Cookie And Authentication
SQL Server Reporting Services- Custom Chart Colors
Looping Through a SQL Result in C#
Automatically Redirect People To Different Landing Pages in PHP
t-SQL Cursor

©2008 AndrewKimball.com