正则表达式匹配括号中的所有数字,但不匹配单词中的数字

时间:2018-12-03 07:16:23

标签: python regex regex-negation

我希望正则表达式匹配方括号内的所有数字,但不匹配单词中以数字开头的数字。

例如,这些是匹配的:

Hi (1)
(100)
Yes (1000)

但是这些不匹配:

One (1)
One hundred (100)
Five thousand (5000)
Twelve (12)

这是我认为正则表达式应该的方式,但是它不起作用。

(\(\d+\)|(?!one[ ]\((\d+)\))|(?!two[ ]\((\d+)\))|(?!three[ ]\((\d+)\))|(?!four[ ]\((\d+)\))|(?!five[ ]\((\d+)\))|(?!six[ ]\((\d+)\))|(?!seven[ ]\((\d+)\))|(?!eight[ ]\((\d+)\))|(?!nine[ ]\((\d+)\))|(?!ten[ ]\((\d+)\))|(?!eleven[ ]\((\d+)\))|(?!twelve[ ]\((\d+)\))|(?!thirteen[ ]\((\d+)\))|(?!fourteen[ ]\((\d+)\))|(?!fifthteen[ ]\((\d+)\))|(?!sixteen[ ]\((\d+)\))|(?!seventeen[ ]\((\d+)\))|(?!eighteen[ ]\((\d+)\))|(?!nineteen[ ]\((\d+)\))|(?!twenty[ ]\((\d+)\))|(?!thirty[ ]\((\d+)\))|(?!forty[ ]\((\d+)\))|(?!fifty[ ]\((\d+)\))|(?!sixty[ ]\((\d+)\))|(?!seventy[ ]\((\d+)\))|(?!eighty[ ]\((\d+)\))|(?!ninety[ ]\((\d+)\))|(?!hundred[ ]\((\d+)\))|(?!thousand[ ]\((\d+)\))|(?!million[ ]\((\d+)\)))

更新:

我想做的是给一个字符串,例如:

Hi (1) (100) Yes (1000) But these are not matched: One (1) One hundred (100) Five thousand (5000) Twelve (12)

我想创建一个仅与方括号中的数字匹配但不与单词中的数字匹配的函数,并返回:

Hi (1)\n (100)\n Yes (1000)\n But these are not matched: One (1) One hundred (100) Five thousand (5000) Twelve (12)'

更新2:

根据评论,这是我可以获得的最佳答案。但是仍然遇到麻烦,因为该组无法正常工作以仅匹配第一个组

def split(s):
    reg = re.compile(r"\b(?:one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifthteen|sixteen|seventeen|eighteen|nineteen|twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety|hundred|thousand|million)+[ ]{0,1}\(\d+\)|\((\d+)\)", re.IGNORECASE)
    s = reg.sub(lambda match: match.group(0) + '\n', s)
    return next(i) + '\n'.join(map(' '.join, zip(i, i)))

0 个答案:

没有答案