I would like to replace all occurrences of 3 or more "=" with an equal-number of "-".
def f(a, b):
'''
Example
=======
>>> from x import y
'''
return a == b
becomes
def f(a, b):
'''
Example
-------
>>> from x import y
'''
return a == b # don't touch
My working but hacky solution is to pass a lambda to repl
from re.sub()
that grabs the length of each match:
>>> import re
>>> s = """
... def f(a, b):
... '''
... Example
... =======
... >>> from x import y
... '''
... return a == b"""
>>> eq = r'(={3,})'
>>> print(re.sub(eq, lambda x: '-' * (x.end() - x.start()), s))
def f(a, b):
'''
Example
-------
>>> from x import y
'''
return a == b
Can I do this without needing to pass a function to re.sub()
?
My thinking would be that I'd need r'(=){3,}'
(a variable-length capturing group), but re.sub(r'(=){3,}', '-', s)
has a problem with greediness, I believe.
Can I modify the regex eq
above so that the lambda isn't needed?