regex to remove ordinals

后端 未结 5 1189
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-30 14:36

I need to remove ordinals via regex, but my regex skills are quite lacking. The following locates the ordinals, but includes the digit just prior in the return value. I need

5条回答
  •  -上瘾入骨i
    2020-11-30 14:46

    You need to use a look-behind assertion so that only st|nd|rd|th preceded by a [0-9] are matched, but the [0-9] isn't included in the match. i.e.:

    (?<=[0-9])(?:st|nd|rd|th)
    

    I've linked to the perl-compatible syntax, but if you're using posix, posix extended, vi or one of many other regex syntaxes you'll need to look up the syntax.

提交回复
热议问题