问题
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