Regular Expressions 101

Community Patterns

remove ordinals

1

Regular Expression
PCRE (PHP <7.3)

/
^(((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?)))$
/
igm

Description

a robust pattern to remove ordinal suffixes from numbers, eg. '22nd'->'22'

supports: singles: 1st, 2nd, 3rd, 4th teens: 11th, 12th, 13th, 14th

Submitted by missinglink - 8 years ago