import re
regex = re.compile(r"location \/api.*?proxy_pass\s*([^\s;]+)", flags=re.DOTALL | re.MULTILINE)
test_str = ("server {\n"
" listen 80;\n"
" server_name localhost;\n\n"
" location /api {\n"
" rewrite /api/(.*) /$1 break;\n"
" proxy_pass https://host.name:1337;\n"
" proxy_redirect off;\n"
" proxy_set_header Host $host;\n"
" }\n"
" location / {\n"
" root /usr/share/nginx/html;\n"
" index index.html index.htm;\n"
" }\n"
"}")
subst = "http://localhost:1234"
result = regex.sub(subst, test_str)
if result:
print(result)
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