I have Python 3.11.9 on Windows 10.
I have many lines in a text file that look like this:
<prices> <price grade="c">700</price> <price grade="d">705</price> <price grade="f">710</price> <price grade="h">715</price> <price grade="i">720</price> <price grade="j">725</price> <price grade="k">726</price> <price grade="l">730</price> <price grade="m">740</price> <price grade="o">745</price> <price grade="p">750</price> <price grade="q">755</price></prices>
I want to prefix all <price
with a CRLF or \n
. They should end up like this:
<prices>
<price grade="c">700</price>
<price grade="d">705</price>
<price grade="f">710</price>
<price grade="h">715</price>
<price grade="i">720</price>
<price grade="j">725</price>
<price grade="k">726</price>
<price grade="l">730</price>
<price grade="m">740</price>
<price grade="o">745</price>
<price grade="p">750</price>
<price grade="q">755</price></prices>
ChatGPT says re.sub
replaces all instances by default, and there is no re.REPLACEALL flag.
In my regex search I have my capture parenthesis. My code is this:
import re
xpptext = '<prices> <price grade="c">700</price> <price grade="d">705</price> <price grade="f">710</price> <price grade="h">715</price> <price grade="i">720</price> <price grade="j">725</price> <price grade="k">726</price> <price grade="l">730</price> <price grade="m">740</price> <price grade="o">745</price> <price grade="p">750</price> <price grade="q">755</price></prices>'
xpptext = re.sub(r'(<price )', '\n\1', xpptext, re.IGNORECASE)
print(xpptext)
Both in my local Python and Attempt This Online I get this which is not right:
<prices>
grade="c">700</price>
grade="d">705</price> <price grade="f">710</price> <price grade="h">715</price> <price grade="i">720</price> <price grade="j">725</price> <price grade="k">726</price> <price grade="l">730</price> <price grade="m">740</price> <price grade="o">745</price> <price grade="p">750</price> <price grade="q">755</price></prices>
Note the first two <price
are not prefixed with \n
but are replaced with \n
even though I have my first capture group in there of \1
(number one digit).
What am I doing wrong here? ATO link, I hope it will work for you.
Thank you.