sub() 缺少 1 个必需的位置参数:'string'

Ali*_*ice -4 python regex

我用 re.verbose 写了这样一个正则表达式

\n\n
#+begin_src ipython :session flask :results output\nimport re\n\ndef clearup(path):\n    fp = open(path, \'r+\')\n    text = fp.read()\n    text = re.sub(r\'\'\'.*PROPERTIES:.*\\n\n                      (?:.*\\n)* # multiple lines in the middle\n                      .*:END: \'\'\',text, flags=re.VERBOSE)\n    fp.seek(0)\n    fp.write(text)\n    fp.close()\n\nclearup("01.foreword.org")\n#+end_src\n
Run Code Online (Sandbox Code Playgroud)\n\n

运行但是报错:

\n\n
 TypeError: sub() missing 1 required positional argument: \xe2\x80\x99string\xe2\x80\x99\n
Run Code Online (Sandbox Code Playgroud)\n\n

既然没有缺少任何参数,那么问题是什么?

\n

opp*_*yer 7

sub 需要三个(不包括标志)变量。第一个是正则表达式模式。第二个是替换正则表达式。第三个是要从中进行 sub 的字符串。我认为这就是您遇到的错误,因为您似乎没有替换模式,而只有文本。