将字母和数字设置为序数 [英] Set together letters and numbers that are ordinal numbers

查看:111
本文介绍了将字母和数字设置为序数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目的是删除数字和序号缩写(st,rd,th,nd)之间的空格.

The purpose is to remove the space between the numbers and ordinal number abbreviation (st,rd,th,nd).

例如,以下数字和缩写应一起形成10th,1st和133rd:

For instance, the following numbers and abbreviations should be together to form 10th, 1st and 133rd:

10   th elementary
1  st grade
133  rd anniversary

但是,不允许将这些其他示例放在一起:

However, these other examples are not allowed to be set together:

abc123 th 33333    rddccc
10 thetree
20 street

为此,我提出了以下正则表达式:

For this purpose I have came out with the following regex:

(?<=[0-9])+\s+(?=(st|nd|rd|th)\b)

但是,它还将前面不允许的字符串也设置在一起.

However it is setting together also the previous not allowed strings.

您知道如何将正确的序号设置在一起吗?

Do you know how can I set together just the correct ordinal numbers?

推荐答案

您可以在正向表述中添加另一部分,以断言除下划线或数字之外的单词字符,或者断言字符串的末尾(如果是)最后一次出现:

You might add another part to the positive lookahead to assert what follows is a word character except an underscore or digit or assert the end of the string in case it is the last occurrence:

(?<=[0-9])\s+(?=(?:st|[rn]d|th)(?: [^\W\d_]|$))

正则表达式演示

请注意,您可以在正向后看后省略+,并且可以将替换时间缩短为[rn]d

Note that you can omit the + after the positive lookbehind and you might shorten the alternation to [rn]d

这篇关于将字母和数字设置为序数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆