之前要用到正则替换,所以就用到了sub这个功能,看着doc里写的云里雾里的,就自己做了做实验
re.sub(pattern, repl, string, count=0, flags=0)
先说功能
Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl.
就是先去找到string之中可以匹配pattern的部分,然后将这些部分替换成repl
再看看参数情况
第一个参数pattern:就是正则式字符串或者正则式对象
第二个参数repl:就是要用什么东西去替换pattern匹配到的字符串
第三个参数string:就是对哪个字符串进行匹配与替换
第四个参数count:就是只替换前几个,如果是0则全部都替换
先来看python doc给的官方示例
pattern = r'def\s+([a-zA-Z_][a-zA-Z_0-9]*)\s*\(\s*\):'
rep = r'static PyObject*\npy_\1(void)\n{'
s = 'def myfunc():'</