# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"\[((\w+)\b([^]]*))\]((?>(?!\[\2\b).|(?R))*)\[\/\2]"
test_str = ("[quote quote=21555]Got some headphones, tried them, and i have sound with them.[/quote]\n\n"
"[quote quote=21574]Yes speaker not working still, last night reballed audio ic, still the same, i checked the resistors you told me all ok, But U3710 speaker amp shorted! speaker was getting hot!! :) removed U3710 and U3700, but cannot reball one, as one pad is gone. are these the same as iphone 4? i know they are different numbers, but would they work?[/quote]\n")
subst = "<blockquote$3>$4</blockquote>"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.DOTALL)
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