# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(?<level>\d+)\s*(?<wood>\d+)\s*(?<clay>\d+)\s*(?<iron>\d+)\s*(?<crop>\d+)\s*(?<cropusage>\d+)\s*(?<time>\d+:\d+:\d+)\s*(?<pop>\d+)\s*(?<production>\d+)"
test_str = ("1 70 90 70 20 0 00:02:30 1 7\n"
"2 115 150 115 35 0 00:07:20 1 13\n"
"3 195 250 195 55 0 00:15:00 2 21\n"
"4 325 420 325 95 0 00:27:30 2 31\n"
"5 545 700 545 155 0 00:47:10 2 46\n"
"6 910 1170 910 260 1 01:18:50 3 70\n"
"7 1520 1950 1520 435 1 02:03:40 4 98\n"
"8 2535 3260 2535 725 1 03:30:40 4 140\n"
"9 4235 5445 4235 1210 1 05:40:30 5 203\n"
"10 7070 9095 7070 2020 1 09:08:00 6 280\n"
"11 11810 15185 11810 3375 1 14:40:10 7 392\n"
"12 19725 25360 19725 5635 1 23:31:40 9 525\n"
"13 32940 42350 32940 9410 1 37:41:50 11 693\n"
"14 55005 70720 55005 15715 1 60:22:20 13 889\n"
"15 91860 118105 91860 26245 1 96:39:10 15 1120\n"
"16 153405 197240 153405 43830 2 154:41:50 18 1400\n"
"17 256190 329385 256190 73195 2 247:34:20 22 1820\n"
"18 427835 550075 427835 122240 2 396:10:10 27 2240\n"
"19 714485 918625 714485 204140 2 633:550: 32 2800\n"
"20\n"
" 1193195 1534105 1193195 340915 2 1014:20:30 38 3430")
subst = "<Level val=\"${level}\"><Resources><Wood>${wood}</Wood><Clay>${clay}</Clay><Iron>${iron}</Iron><Crop>${crop}</Crop><CropUsage>${cropusage}</CropUsage></Resources><UpgradeTime>${time}</UpgradeTime><Population>${pop}</Population><Production>${production}</Production></Level>"
# 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