Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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
带?P的python re.sub()_Python_Regex - Fatal编程技术网

带?P的python re.sub()

带?P的python re.sub(),python,regex,Python,Regex,我可以使用re.sub()的第一个参数中的变量来在第二个参数中使用它吗?让我举例说明: re.sub(r'(?P<id>>>>[0-9]+)', 'sometext(?P=id)sometext', self.text)) re.sub(r'(?P>>>[0-9]+),'sometext(?P=id)sometext',self.text)) 我可以在“sometext(?p=id)sometext”中使用id变量吗?实际上,这个代码不起作用,所以我来了。你可以

我可以使用re.sub()的第一个参数中的变量来在第二个参数中使用它吗?让我举例说明:

re.sub(r'(?P<id>>>>[0-9]+)', 'sometext(?P=id)sometext', self.text))
re.sub(r'(?P>>>[0-9]+),'sometext(?P=id)sometext',self.text))

我可以在“sometext(?p=id)sometext”中使用id变量吗?实际上,这个代码不起作用,所以我来了。

你可以按编号引用一个捕获组,例如第一个捕获组是
\1
你可以按编号引用一个捕获组,例如第一个捕获组是
\1

你读过了吗?
re.sub()
的第二个参数可以是函数。为每个模式匹配调用它,并传递一个匹配对象。它应该返回新字符串,这样您就可以通过这种方式获取变量。您读过了吗?
re.sub()
的第二个参数可以是函数。为每个模式匹配调用它,并传递一个匹配对象。它应该返回新字符串,这样您就可以通过这种方式获取变量。