# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^.+version(.*) character file\.([\n\r]+)(\d+)([^\(]+) \([^\d]+( \d+),.+\n\s+.+as a(.+) on.+\n\s+(?:Was.+One\.\n)?((?:.|\n)+[!.])\n(?:.|\n)+\((\d+)(?:.|\n)+$"
test_str = (" Dungeon Crawl Stone Soup version 0.16-a0-3667-g690a316 (webtiles) character file.\n\n"
"462 8Escape the Ruffian (level 6, -2/47 HPs)\n"
" Began as a Bearkin Transmuter on Jan 31, 2015.\n"
" Slain by an orc\n"
" ... wielding a +0 trident\n"
" (3 damage)\n"
" ... on level 4 of the Dungeon.\n"
" The game lasted 00:28:53 (3698 turns).\n\n"
"8Escape the Ruffian (Bearkin Transmuter) Turns: 3698, Time: 00:28:53")
subst = "Version:$1\\nScore: $3\\nName:$4\\nCharacter:$6\\nLevel:$5\\nCause of Death/Victory: $7\\nTurns: $8"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 1)
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