# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"<child\s+[^<]*?attr2=[\'\"](?<value>[^<]*?)[\'\"]"
test_str = ("Correct Matched:\n"
"<child attr1='1' attr2='asd asddsa' attr3='1'/>\n"
"<child attr1='1' attr2='asd asddsa'attr3='1'/>\n"
"<child attr1='1' attr2='asd asddsa' />\n"
"<child attr1='1' attr2='asd asddsa'/>\n"
"<child attr1='1' attr2='asd \n"
"asddsa'/>\n"
"<child attr1='1' attr2='asd asddsa'></child>\n"
"<child attr1='1\"'' attr2='asd asddsa'><elem/></child>\n"
"<child attr1='1sda-cxc\"'asdcvas<' elem attr2='asd asddsa' attr1=1><elem/></child'>\n"
"<child attr1='1sda-cxc\"'asdcvas<'\n"
" elem attr2='asd asddsa' attr1=1><elem/></child\n"
"</root>\n\n"
"Correctly Not Matched:\n"
"<child attr1='1sda-cxc\"'asdcvas'><elem attr2='asd asddsa' attr1=1><elem/></child>\n\n"
"Bug:\n"
"<child attr1='1sda-cxc\"'asdcvas'> elem attr2='asd asddsa' attr1=1><elem/></child>")
subst = ""
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
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