Use | to combine different case

image_pdfimage_print

using System;
using System.Text.RegularExpressions;

class RXOptionsApp {
static void Main(string[] args) {
Regex r = new Regex(“in|In|IN|iN”);
string s = “The KING Was In His Counting House”;
MatchCollection mc = r.Matches(s);
for (int i = 0; i < mc.Count; i++) { Console.WriteLine("Found '{0}' at position {1}",mc[i].Value, mc[i].Index); } } } [/csharp]

Proper case match

image_pdfimage_print
   
 

using System;
using System.Text.RegularExpressions;


public class RXProperCaseApp {
    static void Main(string[] args) {
        string s = "the qUEEn wAs in HER parLOr";
        s = s.ToLower();
        string e = @"w+|W+";
        string sProper = "";

        foreach (Match m in Regex.Matches(s, e)) {
            sProper += char.ToUpper(m.Value[0])
                + m.Value.Substring(1, m.Length - 1);
        }
        Console.WriteLine("ProperCase:	{0}", sProper);
    }
}

    


Leading space

image_pdfimage_print
   
 

using System;
using System.Text.RegularExpressions;


class RXmodifyingApp {
    static void Main(string[] args) {
        string s = "     leading";
        string e = @"^s+";
        Regex rx = new Regex(e);
        string r = rx.Replace(s, "");
        Console.WriteLine("Strip leading space: {0}", r);
    }
}

    


Define groups 'ing', 'in', 'n'

image_pdfimage_print

using System;
using System.Text.RegularExpressions;

class GroupingApp {
static void Main(string[] args) {
Regex r = new Regex(“(i(n))g”);
Match m = r.Match(“Matching”);
GroupCollection gc = m.Groups;
Console.WriteLine(“Found {0} Groups”, gc.Count);
for (int i = 0; i < gc.Count; i++) { Group g = gc[i]; Console.WriteLine("Found '{0}' at position {1}",g.Value, g.Index); } } } [/csharp]

Gr(a|e)y

image_pdfimage_print

using System;
using System.Text.RegularExpressions;

public class MainClass {
public static void Main() {

Regex n = new Regex(“Gr(a|e)y”);
MatchCollection mp = n.Matches(“Green, Grey, Granite, Gray”);
for (int i = 0; i < mp.Count; i++) { Console.WriteLine("Found '{0}' at position {1}",mp[i].Value, mp[i].Index); } } } [/csharp]

((an)|(in)|(on))

image_pdfimage_print

using System;
using System.Text.RegularExpressions;

public class MainClass {
public static void Main() {

Regex p = new Regex(“((an)|(in)|(on))”);
MatchCollection mn = p.Matches(“King, Kong Kang”);
for (int i = 0; i < mn.Count; i++) { Console.WriteLine("Found '{0}' at position {1}",mn[i].Value, mn[i].Index); } } } [/csharp]

Regex and letter case

image_pdfimage_print

using System;
using System.Text.RegularExpressions;

public class MainClass {
public static void Main() {

Regex q = new Regex(” in “);
MatchCollection mm = q.Matches(“IN at in or”);
for (int i = 0; i < mm.Count; i++) { Console.WriteLine("Found '{0}' at position {1}",mm[i].Value, mm[i].Index); } } } [/csharp]