次のような文字列があります。
" アパートは 2021 年から学生に貸し出されています。毎月の家賃は 850 ユーロです。追加費用は光熱費 (150 ユーロ) です。"
「rent」および「Euro」に近接している (たとえば 20 文字以内) 数値を探しています。
「2021」も「150」も手に入れたくない - 「850」を手に入れたい。
現在、このコードを使用していますが、これで「2021」になります。手伝って頂けますか?
よろしくお願いします!フェリックス
txt = "The apartment is rented out to a student since 2021. The monthly rent is 850 Euro. Additional costs are utilities (150 Euro)."
txt = ("".join(txt)).strip()
m = re.search(r'(?:((?i:rent)|JNKM)[\w\€\:\(\)\.\!\?\-\\,\ ]{0,40}(\d+[\,\.]?\d*)|(?:(\d+[\,\.]?\d*)[\w\€\:\(\)\.\!\?\-\\,\ ]{0,40}((?i:rent)|JNKM)))',"".join(txt))
txtrent = m.group().replace(".","").replace(",",".")
txtrent = re.findall(r"-?\d+[\,\.]?\d*", txtrent )
zustand = txtrent
print(zustand)```