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); } }
Author: coder
Leading space
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'
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
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))
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
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]
Position and index
using System; using System.Text.RegularExpressions; class MatchingApp { static void Main(string[] args) { Regex r = new Regex("in"); Match m = r.Match("Matching"); if (m.Success) { Console.WriteLine("Found '{0}' at position {1}",m.Value, m.Index); } } }