Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams text = fp.read() text = re.sub(r'''.*PROPERTIES:.*\n (?:.*\n)* # multiple lines in the middle .*:END: ''',text, flags=re.VERBOSE) fp.seek(0) fp.write(text) fp.close() clearup("01.foreword.org") #+end_src

Run it but report error:

 TypeError: sub() missing 1 required positional argument: ’string’

What's the problem since there are no arguments missing?

"What's the problem since there are no arguments missing?" - there is an argument missing. Did you read the doc? – zvone Dec 16, 2019 at 1:48 In other words, "What's the problem since there are no arguments missing?`"—there are arguments missing. Read the documentation for the code you're trying to run before saying there are no arguments missing. – Chris Dec 16, 2019 at 1:33

From python document: https://docs.python.org/3/library/re.html#re.sub

re.sub(pattern, repl, string, count=0, flags=0)

quite a lot of arguments, and together with re.VERBOSE, it is so confusing, and the position of the arguments is error-prone.

So I would suggest separating defining pattern and .sub move in to two lines:

pt = re.compile(pattern, flags = re.VERBOSE)
text = pt.sub(repl, string)

which is more readable in my opinion.

If you're still confused about this or if anyone else comes here to read this. The author of the post was probably trying to call the sub function on the created Regex not on re. When you do that, you only need to give 2 args to sub

myRegex = re.compile(.... something.....)
myRegex.sub( <regex pattern with which to replace>, <string that has to be subbed>)
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.