你可以尝试这样的事情:
In [8]: import re
In [14]: strs="This changes are related to book:id:pages:3000 location /file1/file2/file3/pages.000.zip"
In [15]: re.findall(r"\d+[A-Ea-e]{0,}\d+[A-Ea-e]{0,}",strs)
Out[15]: ['3000', '000']
In [16]: strs1="This changes are related to book:id:pages:30ab00e location /file1/file2/file3/pages.000.zip"
In [17]: re.findall(r"\d+[A-Ea-e]{0,}\d+[A-Ea-e]{0,}",strs1)
Out[17]: ['30ab00e', '000']
用于re.sub()
替换:
In [68]: strs="This changes are related to book:id:pages:3000 location /file1/file2/file3/pages.000.zip"
In [69]: re.sub(r"(\d+[A-Ea-e]*\d+[A-Ea-e]*)|(\d+)","my_doc",strs)
Out[69]: 'This changes are related to book:id:pages:my_doc location /filemy_doc/filemy_doc/filemy_doc/pages.my_doc.zip'
In [70]: strs1="This changes are related to book:id:pages:30ab00e location /file1/file2/file3/pages.000.zip"
In [71]: re.sub(r"(\d+[A-Ea-e]*\d+[A-Ea-e]*)|(\d+)","my_doc",strs1)
Out[71]: 'This changes are related to book:id:pages:my_doc location /filemy_doc/filemy_doc/filemy_doc/pages.my_doc.zip'
In [72]: foo=" number of pages completed, 2 still pending"
In [73]: re.sub(r"(\d+[A-Ea-e]*\d+[A-Ea-e]*)|(\d+)","my_doc",foo)
Out[73]: ' number of pages completed, my_doc still pending'