# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"([\s\S]+)\s*primary\s+key\s*\(\s*`([a-z]\w*)`\s*\)\s*(,\s*unique\s+key\s+uniq_\w+\s*\(`([a-z]\w*)`\))?(,\s*key\s+idx_\w+\s*\((\s*,?\s*`([a-z]\w*)`\s*)+\)\s*)+"
test_str = ("`id` INT (11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'primary',\n"
"`stu_name` VARCHAR (10) NOT NULL DEFAULT '' COMMENT 'username',\n"
"`stu_class` VARCHAR (10) NOT NULL DEFAULT '' COMMENT 'class',\n"
"`stu_num` INT (11) NOT NULL DEFAULT '0' COMMENT 'study number',\n"
"`stu_score` SMALLINT UNSIGNED NOT NULL DEFAULT '0' COMMENT 'total',\n"
"`tuition` DECIMAL (5, 2) NOT NULL DEFAULT '0' COMMENT 'fee',\n"
"`phone_number` VARCHAR (20) NOT NULL DEFAULT '0' COMMENT 'mobile',\n"
"`create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'record created time',\n"
"`update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'record updated time',\n"
"`status` TINYINT NOT NULL DEFAULT '1' COMMENT 'some comment',\n"
"PRIMARY KEY (`id`),\n"
"UNIQUE KEY uniq_stu_num (`stu_num`),\n"
"KEY idx_stu_score (`stu_score`),\n"
"KEY idx_update_time_tuition (`update_time`, `tuition`)")
subst = "\\2"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.IGNORECASE)
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