With Python, I am trying to use re.sub to replace some items in svg_text
Expected Output: Substitute RoundShapes_group_match.group() with classes_removed_text in svg_text
Actual Output: svg_text stays the same -- RoundShapes_group_match.group() remains unchanged
Here is the code. I know people suggest lxml but I can't get that to work either and would still like to figure out why the re.sub isn't working.
I think it has to do with the RoundShapes_group_match.group() because even if I try to replace with ' ' instead of classes_removed_text it doesn't work.
Question:
My python spacing is correct, and it is not throwing errors... it doesn't look correct on Stack Overflow though...
import re
def remove_RoundShapes_group_classes(svg_text):
"""
Within <g id="RoundShapes"> and before SquareShapes, remove all path class names. Do not modify class names
in other groups outside of RoundShapes.
"""
# RoundShapes_group_match = re.search(r"""<g\s+id\s*=\s*"\S*?RoundShapes.*?</g>""", svg_text, flags=re.I | re.DOTALL)
# Change to SquareShapes to search to next group
RoundShapes_group_match = re.search(r"""<g\s+id\s*=\s*"\S*?RoundShapes.*?SquareShapes""", svg_text,
flags=re.I | re.DOTALL)
if RoundShapes_group_match is None:
print("Could not find the RoundShapes group.", file=stderr)
return svg_text
classes_removed_text = re.sub(r'''class\s*=\s*".*?"''', '', RoundShapes_group_match.group(), flags=re.I)
# print(classes_removed_text)
RoundShapes_to_replace = (RoundShapes_group_match.group(0))
##RoundShapes_group_match.group() is what is not working. Are there extra returns?
svg_text = re.sub(RoundShapes_group_match.group(), classes_removed_text, svg_text, 1)
return svg_text
I don't know why this resolved the issue, but instead of using re.sub I used replace
svg_text = svg_text.replace(RoundShapes_group_match.group(0), classes_removed_text)
return svg_text
It now works!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.