| 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
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
Use Multple Regex OptionsRegexOptions.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: Separate multiple regexoptions with a vertical bar "|". e.g. RegexOptions.IgnoreCase | RegexOptions.Multiline
Example Regular ExpressionsGrab HTML - 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: ( | |||||||||||||||||||