当数字后跟字母模式时从字符串中删除数字
Remove digits from a string when they are followed by a letter pattern
我有如下三个字符串
ex1 = "All is good 24STREET"
ex2 = "Is this the weight 2.5OZ"
ex3 = "Feeling good 100pc"
我只想删除后跟“OZ”或“pc”的数字,但不删除其他数字。
**results**
ex1 = "All is good 24STREET"
ex2 = "Is this the weight OZ"
ex3 = "Feeling good pc"
我尝试使用 'str.replace('\d+', '')' 但这会删除所有数字,也不会删除“点”
import re
ex1 = "All is good 24STREET"
ex2 = "Is this the weight 2.5OZ"
ex3 = "Feeling good 100pc"
reg = re.compile(r'[\d.]+(?=OZ|pc)')
print(reg.sub('', ex1))
print(reg.sub('', ex2))
print(reg.sub('', ex3))
输出:
All is good 24STREET
Is this the weight OZ
Feeling good pc
我有如下三个字符串
ex1 = "All is good 24STREET"
ex2 = "Is this the weight 2.5OZ"
ex3 = "Feeling good 100pc"
我只想删除后跟“OZ”或“pc”的数字,但不删除其他数字。
**results**
ex1 = "All is good 24STREET"
ex2 = "Is this the weight OZ"
ex3 = "Feeling good pc"
我尝试使用 'str.replace('\d+', '')' 但这会删除所有数字,也不会删除“点”
import re
ex1 = "All is good 24STREET"
ex2 = "Is this the weight 2.5OZ"
ex3 = "Feeling good 100pc"
reg = re.compile(r'[\d.]+(?=OZ|pc)')
print(reg.sub('', ex1))
print(reg.sub('', ex2))
print(reg.sub('', ex3))
输出:
All is good 24STREET
Is this the weight OZ
Feeling good pc