gpt4 book ai didi

python - 提取大写前的元素

转载 作者:行者123 更新时间:2023-12-04 23:35:20 24 4
gpt4 key购买 nike

我有以下字符串:

text = '6 rue du Bras de Fer MONTPELLIER France'

我只想提取 "6 rue du Bras de Fer"
我正在尝试使用正则表达式来提取这部分,这是我的正则表达式代码:
re.findall(r'(.*)[A-Z]+',text)

但它返回给我:
['6 rue du Bras de Fer MONTPELLIER ']

最佳答案

为此进行了前瞻:

re.findall(r'^(.*)(?=\s+[A-Z]{2})',text)

这在空格之后的至少两个大写字母之前匹配。

关于python - 提取大写前的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59491468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com