目的是将基数和序数保留在字符串的开头,只要它们紧接在单词PERFORMANCE或之前SCORE:#These numbers are kept:100 SCORE FOR STUDENT80 PERFORMANCE FOR TEACHER但是,如果数字在开头并且以下单词不同,则应将其删除:#These numbers are removed10095TH 10097TH 179TH SCHOOL ANIVERSARY11 12 10 SECONDARY LEVELS100 100 100 100 SCHOOL AGREEMENT我遇到的问题是在单词之前PERFORMANCE或SCORE有空格分隔的数字时:#All numbers should be kept3 10 100 PERFORMANCE001 10 12345 SCORE我正在应用以下正则表达式,但最后一部分很混乱(?!\s*\d*\s*\d*\s*(?:PERFORMANCE|SCORE)\b),因为目前这只是考虑之前PERFORMANCE或SCORE要保留的 3 组数字:(?<=[A-Za-z]\b )([ 0-9]*(ST|[RN]D|TH)?\b)|^(([\d ]+(ST|[RN]D|TH)?)*\b)(?!\s*\d*\s*\d*\s*(?:PERFORMANCE|SCORE)\b)以前的正则表达式适用于以下情况:3 10 100 PERFORMANCE001 10 12345 SCORE但如果我添加一组额外的数字,它将不起作用:3 10 100 1 PERFORMANCE001 10 1 12345 SCORE如何推广此规则以包含所有数字集?
1 回答
Cats萌萌
TA贡献1805条经验 获得超9个赞
尝试以下操作:
^(?:\d+(?:ST|[RN]D|TH)?\s)+(?=[^\d]+$)(?!PERFORMANCE|SCORE)
^ anchor to beginning
(?: start non-capturing group
\d+ match one or more digits
(?:ST|[RN]D|TH)? optionally followed by one of your approved suffixes
\s then a whitespace
)+ one or more times
(?=[^\d]+$ assert that the rest of the line is number-free (forces the regex to not backtrack to the last number)
(?!PERFORMANCE|SCORE) assert that the following characters are NOT 'PERFORMANCE' or 'SCORE'
添加回答
举报
0/150
提交
取消