我想从一个字符串中删除数字,该字符串正好放在单词后面,没有任何空格。例如:'Senku Ishigami is charecter from a manga series98 onging since 2017.'应该:'Senku Ishigami is charecter from a manga series onging since 2017.'我可以使用正则表达式删除检测数字'[a-z]+[0-9]+',但是当我无法理解如何删除它时。我试图通过编写来删除它'[a-z]',因为我认为它会起作用,但它只是'[a-z]'作为字符串打印。这是代码:import retext ='Senku Ishigami is charecter from a manga series98 onging since 2017.'text = re.sub(r'[a-z]+[0-9]+', '[a-z]', text)print(text)输出:Senku Ishigami is charecter from a manga [a-z] onging since 2017.
2 回答
一只斗牛犬
TA贡献1784条经验 获得超2个赞
您可以使用
import re
text ='Senku Ishigami is charecter from a manga series98 onging since 2017.'
text = re.sub(r'(?<=[a-z])\d+\b', '', text)
print(text) # => Senku Ishigami is charecter from a manga series onging since 2017.
正则表达式详细信息
(?<=[a-z])
- 紧邻小写 ASCII 字母的位置\d+
- 一位或多位数字\b
- 单词边界(数字仅在单词末尾匹配)。
添加回答
举报
0/150
提交
取消