using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"^(((1)st?|(2)nd?|(3)rd?|([4-9])th?)|(([0-9]*)(1[0-9])th?)|(([0-9]*[02-9])((1)st?|(2)nd?|(3)rd?|([04-9])th?)))$";
string substitution = @"$3$4$5$6$8$9$11$13$14$15$16";
string input = @"// ordinals
1st
22nd
333rd
4444th
2500th
// teens
11th
12th
13th
14th
15th
16th
17th
18th
19th
// teens - hundreds
111th
112th
113th
114th
115th
116th
117th
118th
119th
// teens - wrong suffix
11st
12nd
13rd
111st
112nd
113rd
// uppercase
1ST
22ND
333RD
444TH
// wrong suffix (do nothing)
0th
26st
31th
21rd
29nd";
RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Multiline;
Regex regex = new Regex(pattern, options);
string result = regex.Replace(input, substitution);
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for C#, please visit: https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex(v=vs.110).aspx