Python keeps returning a string with a broken character.
python
test = re.sub('handle(.*?)', '<verse osisID="lol">\1</verse>', 'handle a bunch of random text here.')
print test
what I want
<verse osisID="lol">a bunch of random text here.</verse>
what i am getting
<verse osisID="lol">*broken character*</verse>a bunch of random text here.
You should either escape the
\character or use ar''raw string:Without the
r''raw string literal, backslashes are interpreted as escape codes. You can double the backslash as well:Note that you replace just the word
handlethere, the.*?pattern matches 0 characters at minimum. Remove the question mark and it’ll match your expected output: