Since there are simpler ways to achieve your goal, you can use them.
As you have already noticed, your replacement function receives a matching object as an argument.
This object has, among other things, the group() method, which can be used instead:
def dashrepl(matchobj): return matchobj.group(0) + ' '
which will give exactly your result.
But you are absolutely right - the documents are a little confused this way:
they describe the argument argument repl :
repl can be a string or a function; if it is a string, any backslashes in it are processed.
and
If repl is a function, it is called for each non-overlapping occurrence of the pattern. The function takes one argument of the mapping object and returns a replacement string.
You could interpret this as if the "replacement string" returned by the function is also applicable to the backslash scrolling process.
But since this process is described only for the case that "this is a string", it becomes clearer, but not obvious at first glance.
source share