# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"{\"en\": {0,1}(\".+), {0,1}\"\w\w\": {0,1}(\".+\")},{0,1}"
test_str = ("{\"en\": \"Male\", \"es\": \"Macho\"},\n"
"{\"en\": \"Female\", \"es\": \"Hembra\"},\n"
"{\"en\":\"Population\",\"es\":\"Población\"},\n"
"{\"en\": \"% of total population\", \"es\": \"% de la población total\"},\n"
"{\"en\": \"All cancers\", \"es\": \"Todos los cánceres\"},\n"
"{\"en\": \"All cancers but C44\", \"es\": \"Todos los cánceres pero C44\"},\n"
"{\"en\": \"Number of cases\", \"es\": \"Numero de casos\"},\n"
"{\"en\": \"Age Group\", \"es\": \"Grupo de edad\"},\n"
"{\"en\": \"Year\", \"es\": \"Año\"},\n"
"{\"en\": \"years\", \"es\": \"años\"},\n"
"{\"en\": \"years old\", \"es\": \"años\"},\n"
"{\"en\": \"Age-standardized incidence rate per\", \"es\": \"Tasa de incidencia estandarizada por edad por\"},\n"
"{\"en\": \"Cumulative incidence risk (percent)\", \"es\": \"Riesgo de incidencia acumulada (porcentaje)\"},\n"
"{\"en\": \"cancer sites\", \"es\": \"sitios de cáncer\"},\n"
"{\"en\": \"Time trend analysis need at least 2 years data\", \"es\": \"El análisis de tendencia temporal necesita al menos 2 años de datos\"},\n"
"{\"en\": \"Age at diagnosis\", \"es\": \"Edad al diagnóstico\"},\n"
"{\"en\": \"Estimated annual percentage change\", \"es\": \"Cambio porcentual anual estimado\"},\n"
"{\"en\": \"Regional registries\", \"es\": \"Registros regionales\"}")
subst = "\\1;\\2"
# 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