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_String - Fatal编程技术网

Python 使用正则表达式进行编辑

Python 使用正则表达式进行编辑,python,regex,string,Python,Regex,String,所以我有一个字符串,我也有一个正则表达式。我只想在字符串中的一个数字上加8。我想知道如何更改组的值,然后将匹配对象返回到字符串 下面是我想做的一个例子 m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist") m.group(1) = 'john' //then some how return the value back to a string 因此字符串将是物理学家约翰·牛顿,您需要使用,而不是重新匹配,并在repl中使用从匹配中捕获

所以我有一个字符串,我也有一个正则表达式。我只想在字符串中的一个数字上加8。我想知道如何更改组的值,然后将匹配对象返回到字符串

下面是我想做的一个例子

m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
m.group(1) = 'john'
//then some how return the value back to a string 
因此字符串将是物理学家约翰·牛顿,您需要使用,而不是重新匹配,并在repl中使用从匹配中捕获的组。例如:

>>> import re
>>> re.sub(r"(\w+) (\w+)", r"john \2", "Isaac Newton, physicist")
'john Newton, physicist'

这里的rjohn\2表示用单词“john”和模式中的第二个捕获组替换匹配项。

正确的输出是什么?要更改带有正则表达式的字符串,必须使用re.submaking-groups-writeable,这将是一个很大的问题。考虑嵌套分组AB,然后执行GROP0=“DEF”;第1组=ghi。最后的字符串是什么?通过更改group0,您已经销毁了匹配/创建group1的文本。