import re
regex = re.compile(r"(\S)<\/[^>]*>\s*<[^>]*>(\S)")
test_str = ("<Inventory>\n"
" <Item>\n"
" <Name>Super Mario Bros</Name>\n"
" <Count>14</Count>\n"
" <Price>29,99</Price>\n"
" <Comment>-No Comment-</Comment>\n"
" <Artist>N/A</Artist>\n"
" <Publisher>Nintendo</Publisher>\n"
" <Genre>Video Games</Genre>\n"
" <Year>1985</Year>\n"
" <ProductID>001</ProductID>\n"
" </Item>\n"
" <Item>\n"
" <Name>The Legend of Zelda</Name>\n"
" <Count>12</Count>\n"
" <Price>34,99</Price>\n"
" <Comment>-No Comment-</Comment>\n"
" <Artist>N/A</Artist>\n"
" <Publisher>Nintendo</Publisher>\n"
" <Genre>Video Games</Genre>\n"
" <Year>1986</Year>\n"
" <ProductID>002</ProductID>\n"
" </Item>\n"
"</Inventory>")
subst = "$1\",\"$2"
result = regex.sub(subst, test_str)
if result:
print(result)
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html