Set together letters and numbers that are ordinal numbers

梦想与她 提交于 2020-03-02 09:14:31

问题


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

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?


回答1:


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_]|$))

Regex demo

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



来源:https://stackoverflow.com/questions/59902994/set-together-letters-and-numbers-that-are-ordinal-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!