gpt4 book ai didi

长复杂字符串的Python正则表达式

转载 作者:太空宇宙 更新时间:2023-11-04 06:07:34 24 4
gpt4 key购买 nike

所以我从网页上抓取数据,接收到的数据通常如下:

233989 001 0/49 T R 4:15 PM - 5:30 PM 205 IST Building 01/13/14 - 05/02/14 Controls View (814) 865-8947 266200 002 0/43 M W F 10 :10 AM - 11:00 AM 110 IST Building 01/13/14 - 05/02/14 Controls View (814) 865-8947

我正在尝试将数据从模式######(6 个数字,即 233989)拆分为代表当前数据行末尾的电话号码(即 (814) 865-8947) 因为我知道它总是以 4 个数字结尾,所以我想出了这个表达式:

(^[0-9]{1,6}$[^[0-9]{1,4}$]*[0-9]{1,4}$+)+

虽然这似乎不起作用。谁能伸出援手?

最佳答案

你可以使用这个:

r'(\d{6}.*?\(\d{3}\) \d{3}-\d{4}) ?'

然后在$1\n 上重建它

像这样:http://regex101.com/r/lG4gG5

python :

import re

s = '233989 001 0 / 49 T R 4:15 PM - 5:30 PM 205 IST Building 01/13/14 - 05/02/14 Controls View (814) 865-8947 266200 002 0 / 43 M W F 10:10 AM - 11:00 AM 110 IST Building 01/13/14 - 05/02/14 Controls View (814) 865-8947'
spl = re.split(r'(\d{6}.*?\(\d{3}\) \d{3}-\d{4}) ?', s)
for line in spl:
print line

关于长复杂字符串的Python正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21126728/

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