text='我叔叔住院了。\n他已经病了10个月。\n医院地址是\n海得拉巴朱比利山纳波利健康城校区-500 033。'
现在我想使用regex lookback提取医院地址,即“阿波罗健康城校区,朱比利山,海得拉巴-**”,我使用了以下代码,但我需要一个表达式,可以帮助我提取字符串,直到六位pincode之前的\n字符,即“500 033” 目前我正在尝试提取6个字符串,但我需要一个正则表达式,它可以帮助我获取所有字符串,直到'\n'
r'((\w\S+\s+){1,6})(?=500 033)'
预期输出-海得拉巴朱比利山阿波罗健康城校区-即之前的所有字符串\n
为什么不简单地使用
\n
分割并获取最后一个字符串呢如果您确定字符串将包含pincode。i、 e 6位数字,另一种方法可以是:
我只在字符串中添加了6位数字的支票。您可以根据需要进行修改
相关问题 更多 >
编程相关推荐