正则表达式删除序数 [英] regex to remove ordinals
问题描述
我需要通过正则表达式删除序数,但我的正则表达式技能非常缺乏.以下内容定位序数,但包括返回值中的前面的数字.我只需要隔离和删除序数.
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 to isolate and remove just the ordinal.
[0-9](?:st|nd|rd|th)
推荐答案
您需要使用 后视断言 以便只匹配[0-9]
前面的st|nd|rd|th
,但>[0-9]
不包含在匹配中.即:
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)
我已链接到与 perl 兼容的语法,但如果您使用的是 posix、posix 扩展、vi 或许多其他正则表达式语法之一,则您需要查找语法.
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.
这篇关于正则表达式删除序数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!