Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 找到数字并替换它_Python_Regex - Fatal编程技术网

Python 找到数字并替换它

Python 找到数字并替换它,python,regex,Python,Regex,我有一根这样的线: This changes are related to book:id:pages:3000 location /file1/file2/file3/pages.000.zip This changes are related to book:id:pages:30ab00e location /file1/file2/file3/pages.000.zip 在这里,我想用“my_doc”替换数字(有时数字也是十六进制的) 我试过: match = re.findall(

我有一根这样的线:

This changes are related to book:id:pages:3000 location /file1/file2/file3/pages.000.zip
This changes are related to book:id:pages:30ab00e location /file1/file2/file3/pages.000.zip
在这里,我想用“my_doc”替换数字(有时数字也是十六进制的) 我试过:

 match = re.findall("[\.0-9]*",text)
print match
但它只适用于数字或数字,也适用于十六进制数字,并将数字替换为“my_doc”,然后打印整行 输出:


考虑正则表达式中的条件:

您可以尝试以下方法:

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'
这是疯狂的(你的问题也是如此)和刻薄的

十六进制字符(a-z,a-z)出现在字符串中的许多位置,因此这些字符将被替换(虽然问题不反对atm;))似乎不是预期的行为

假设要删除的blob/部分是十六进制字,并且假设它的最小长度是3,请考虑:

import re
from string import hexdigits


str_1 = "This changes are related to book:id:pages:3000 location /file1/file2/file3/pages.000.zip"

str_2 = "This changes are related to book:id:pages:30ab00e location /file1/file2/file3/pages.000.zip"

expression = '[%s]{3,}'%(string.hexdigits)  # = '[' + hexdigits + ']{3,}'
re.sub(exp, 'my_doc', str_1)
编辑:好的,少一点疯狂的正则表达式,使用下面的表达式

expression = ':[%s]+\S'%(hexdigits)

这将只匹配十六进制单词,因此十六进制+数字的长度不再是一个限制。

但相同的正则表达式在以下情况下无效:完成的页数,2仍在等待我想做一个通用的,将所有的数字和十六进制也…我如何替换它,并获得所提到的输出above@SuryaGupta您可以在这里使用
re.sub()
来获得预期的输出,并处理您提到的两个示例。我同意。。。但是我想要一个通用的正则表达式,它可以处理所有包含数字和十六进制数的字符串,但不幸的是它不能处理这个字符串“已完成的页数,2仍在等待中”@SuryaGupta很高兴能帮到你,如果它对你有用,你可以点击勾号。如果我们在这个字符串上尝试同样的方法“Books:pages[0-9]6:未完成”则输出不正确,如“书籍:页面我的文档:未完成”
expression = ':[%s]+\S'%(hexdigits)