# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(\"(?:\\.|[^\"\\])*\"|'(?:\\.|[^'\\])*'|(?:[^/\n\"']|/[^/*\n\"'])+|\n)|(/\* (?:[^*]|\*[^/])*\*/)|(?://(.*)$)$"
test_str = ("//Bienvenue sur ma carte!\n"
"//Par Samuel \"Déjeuner d'aujourd'hui\" Lapointe\n"
"MAP {\n"
" CONFIG: 'ON_MISSING_DATA' 'IGNORE'\n"
" ##CONFIG: \"MS_ERRORFILE\" \"../debugFile.log\"\n"
" CONFIG: 'PROJ_LIB' '../'\n"
" FONTSET: '../fonts.lst'\n"
" IMAGETYPE: png\n"
" MAXSIZE: 4000\n"
" SIZE: 800 800\n"
" UNITS: meters\n"
" EXTENT: -20405648.939901 -17712669.979681 20314497.045109 19408951.476421\n"
" IMAGECOLOR: '#C6E2F2'\n"
" SHAPEPATH: '../pdata/natural_earth/'\n"
" WEB {\n"
" METADATA {{\n"
" \"ows_enable_request\" \"*\"\n"
" \"wms_srs\" \"EPSG:900913 EPSG:4326 EPSG:3857\"\n"
" \"labelcache_map_edge_buffer\" \"10\"\n"
" \"wms_title\" \"grid\"\n"
" \"wms_onlineresource\" \"http://mapserver.com:8081/cgi-bin/mapserv?map=/opt/scribeui/workspaces/blabla.map\" //Un lien!\n"
" }}\n"
" IMAGEPATH: '/tmp/ms_tmp/'\n"
" IMAGEURL: '/ms_tmp/'\n"
" }\n"
" DEBUG: 5\n"
" PROJECTION {{\n"
" \"init=epsg:900913\"\n"
" }}\n"
" INCLUDE: '../symbols.map'\n"
"}//C'est la fin!\n")
subst = "\\1"
# 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