Python正则表达式查找接近特定单词(前后)的数值
我有一个字符串如下所示: “公寓自2021年起出租给学生。月租金为850欧元。额外费用为水电费(150欧元)。” 我希望找到与“租金”和“欧元”非常接近的数值(例如20个字符以内) 我不想得到“2021”,也不想得到“150”-我想得到“850” 目前我正在使用这段代码,但有了这段代码,我就得到了“2021”。你能帮我吗 提前多谢! 费利克斯 看看这个:Python正则表达式查找接近特定单词(前后)的数值,python,regex,or-operator,and-operator,Python,Regex,Or Operator,And Operator,我有一个字符串如下所示: “公寓自2021年起出租给学生。月租金为850欧元。额外费用为水电费(150欧元)。” 我希望找到与“租金”和“欧元”非常接近的数值(例如20个字符以内) 我不想得到“2021”,也不想得到“150”-我想得到“850” 目前我正在使用这段代码,但有了这段代码,我就得到了“2021”。你能帮我吗 提前多谢! 费利克斯 看看这个: txt = "The apartment is rented out to a student since 2021. The m
txt = "The apartment is rented out to a student since 2021. The monthly rent is 850 Euro. Additional costs are utilities (150 Euro)."
txt = txt.replace('.', '')
pattern = '\s'
result = re.split(pattern, txt)
txt = result[result.index('rent'): result.index('Euro')+1]
for i in txt:
if i.isdigit():
print(i)
如果必须有
欧元
,为什么不能有\b(?:(?i:rent)| JNKM)\b\D{0,40}(\D+(?:[,.]\D+)\D{0,40}\b(?i:euro)\b
?请参阅。这超出了堆栈溢出问题的范围,我认为正则表达式不是此工作的正确工具。你可以展示更多的例子,或者链接你的完整数据集吗?
txt = "The apartment is rented out to a student since 2021. The monthly rent is 850 Euro. Additional costs are utilities (150 Euro)."
txt = txt.replace('.', '')
pattern = '\s'
result = re.split(pattern, txt)
txt = result[result.index('rent'): result.index('Euro')+1]
for i in txt:
if i.isdigit():
print(i)