Python正则表达式使用re.sub公司清理绳子

2024-05-12 23:33:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我在使用regex sub从字符串中删除数字时遇到一些问题。输入字符串可以如下所示:

"The Term' means 125 years commencing on and including 01 October 2015."

"125 years commencing on 25th December 1996"

"the term of 999 years from the 1st January 2011"

我要做的是删除数字和单词'years'-我也在使用DateFinder来分析日期字符串,但是{}将数字解释为日期-因此我要删除数字。在

regex表达式删除数字和单词'years'有什么想法吗?在


Tags: andthe字符串on数字单词regexmeans
2条回答

尝试删除数字和单词years

re.sub(r'\s+\d+|\s+years', '', text)

例如,如果:

^{pr2}$

则输出为:

"The Term' means commencing on and including October."

我想这是你想要的:

import re

my_list = ["The Term' means 125 years commencing on and including 01 October 2015.",
"125 years commencing on 25th December 1996",
"the term of 999 years from the 1st January 2011",
]

for item in my_list:
    new_item = re.sub("\d+\syears", "", item)
    print(new_item)

结果:

^{pr2}$

注意,你最后会有一些额外的空白(也许你想要)?但你也可以把这个加到“清理”中:

new_item = re.sub("\s+", " ", new_item)

因为我喜欢regex:new_item=re.sub公司(“^\s+|\s+$”,“”,新项)

new_item = new_item.strip()

相关问题 更多 >