# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"\{\"contents\":\{\"name\":\"(.*)\",\"rarity\":\"(\d*)\",\"base_attack\":\"(\d*)\",\"base_defense\":\"(\d*)\",\"base_magic_attack\":\"(\d*)\",\"base_magic_defense\":\"(\d*)\",\"base_speed\":\"(\d*)\",\"base_luck\":\"(\d*)\",\"hi_level\":\"?(\d*)\"?,\"hi_attack\":(\d*),\"hi_defense\":(\d*),\"hi_magic_attack\":(\d*),\"hi_magic_defense\":(\d*),\"hi_speed\":(\d*),\"hi_luck\":(\d*),\"((weapon)|(protector)|(accessoire))_id\":\"\d*\",\"description\":\"([^\"*]*)(?:\*([^\"]*))?\",\"\g{16}_image\":\"([^\"]*)\",\"sale_price\":\"\d*\"\},\"header\":\{\"status\":\"OK\",\"error_msg\":null\}\}"
test_str = "{\"contents\":{\"name\":\"Devil\\u2019s Rage Hammer\",\"rarity\":\"5\",\"base_attack\":\"43\",\"base_defense\":\"0\",\"base_magic_attack\":\"1\",\"base_magic_defense\":\"0\",\"base_speed\":\"0\",\"base_luck\":\"0\",\"hi_level\":65,\"hi_attack\":117,\"hi_defense\":0,\"hi_magic_attack\":4,\"hi_magic_defense\":0,\"hi_speed\":0,\"hi_luck\":0,\"weapon_id\":\"11269\",\"description\":\"A hammer that symbolizes rage, one of many carried by the Brimstone Commander of the legions of mankind's natural enemy, the Demon King. The weapon transforms the wielder\\u2019s anger into power, it\\u2019s power is limitless as their rage burns hotter and hotter. Guess that\\u2019s why it was such a good match for the old Commander.\",\"weapon_image\":\"http:\\/\\/cdn.girlskingdom.nutaku.net\\/img\\/icon\\/item\\/large\\/i188_01_l.png\",\"sale_price\":\"4800\"},\"header\":{\"status\":\"OK\",\"error_msg\":null}}"
subst = "[template]Hitsuji:TemplateEquipment\\r\\n | Name = ${1}\\r\\n | Image = ${22}\\r\\n | Rarity = ${2}\\r\\n | Type = (?{17}Weapon)(?{18}Armor)(?{19}Accessoire)\\r\\n | Class = Universal\\r\\n | Description = ${20}\\r\\n | Effect = ${21}\\r\\n | HP level 1 =\\r\\n | MP level 1 =\\r\\n | Attack level 1 = ${3}\\r\\n | Defense level 1 = ${4}\\r\\n | Magic level 1 = ${5}\\r\\n | Resistance level 1 = ${6}\\r\\n | Agility level 1 = ${7}\\r\\n | Luck level 1 = ${8}\\r\\n | Level = ${9}\\r\\n | HP level X =\\r\\n | MP level X =\\r\\n | Attack level X = ${10}\\r\\n | Defense level X = ${11}\\r\\n | Magic level X = ${12}\\r\\n | Resistance level X = ${13}\\r\\n | Agility level X = ${14}\\r\\n | Luck level X = ${15}\\r\\n[/template]"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
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