【发布时间】:2017-05-10 10:13:54
【问题描述】:
我尝试使用 python3 正则表达式获取格式化字符串 - 重新
我的意见:
{'factorial.2.0.0.zip', 'Microsoft ASP.NET Web API 2.2 Client Libraries 5.2.3.zip', 'Newtonsoft.Json.9.0.1.zip'}
我尝试只获取包的名称和版本,如下所示:
- factorial.2.0.0.zip
- 阶乘
- 2.0.0
- Microsoft ASP.NET Web API 2.2 客户端库 5.2.3.zip
- Microsoft ASP.NET Web API 2.2 客户端库
- 5.2.3
等等。 这是我的代码
if diff is not None:
for values in diff.values():
for value in values:
temp = ''
temp1 = ''
temp = re.findall('[aA-zZ]+[0-9]*', value) #name pack
temp1 = re.findall('\d+', value) #version
print(temp)
print(temp1)
我的错误输出:
temp:
['Microsoft', 'ASP', 'NET', 'Web', 'API', 'Client', 'Libraries', 'zip']
['Newtonsoft', 'Json', 'zip']
['factorial', 'zip']
temp1:
['2', '0', '0']
['2', '2', '5', '2', '3']
['9', '0', '1']
右输出:
temp:
['Microsoft', 'ASP', 'NET', 'Web', 'API', 'Client', 'Libraries']
['Newtonsoft', 'Json']
['factorial']
temp1:
['2', '0', '0']
['5', '2', '3']
['9', '0', '1']
我如何解决问题,删除“zip”是搜索和额外的数字。也许有另一种方式解决了我的问题。
【问题讨论】:
-
我强烈建议您删除无意义的标识符,例如 temp,无论您如何更改其他内容。
标签: python regex string python-3.x search