Sunday, December 14, 2014

Regular Expression - replace group

I have a html
<a href="cc.ccs"/><a href="bb.ccs"/><a href="#"/>

I want to replace the non # href so that it become
<a href="my_cc.ccs"/><a href="my_bb.ccs"/><a href="#"/>

print re.sub(
r"href=\"([^#].*)\"", 
r"href=\"my_\g<1>",
r"<a href=\"my_cc.ccs\"/><a href=\"my_bb.ccs\"/><a href=\"#\"/>")

No comments:

Post a Comment