re = /^(((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?)))$/im
str = '// 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'
subst = '$3$4$5$6$8$9$11$13$14$15$16'
result = str.gsub(re, subst)
# Print the result of the substitution
puts result
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 Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html