import re
regex = re.compile(r"^(\s*- \[([0-9]+\.)+ [^]]+\]\(#)([^)]+)\)$", flags=re.MULTILINE)
test_str = ("- [1. Why ?](#1-why-)\n"
"- [2. Compile tool](#2-compile-tool)\n"
"- [3. compile command help](#3--compile-command-help)\n"
" - [3.1. .framework-config environment variables](#31-framework-config-environment-variables)\n"
" - [3.2. Template variables](#32-template-variables)\n"
" - [3.3. Bash-tpl templating](#33-bash-tpl-templating)\n"
" - [3.4. Bash-tpl macros](#34-bash-tpl-macros)\n"
" - [3.4.1. dynamicTemplateDir](#341-dynamictemplatedir)\n"
" - [3.4.2. dynamicSrcFile](#342-dynamicsrcfile)\n"
" - [3.4.3. dynamicSrcDir](#343-dynamicsrcdir)\n"
" - [3.5. directives and template](#35-directives-and-template)\n"
" - [3.5.1. `# FUNCTIONS` directive](#351--functions-directive)\n"
" - [3.5.2. `VAR_*` directive (optional)](#352-var_-directive-optional)\n"
" - [3.5.3. `BIN_FILE` directive (optional)](#353-bin_file-directive-optional)\n"
" - [3.5.5. `EMBED` directive (optional)](#355-embed-directive-optional)\n"
" - [3.5.4. Compiler - Embed::embed](#354-compiler---embedembed)\n"
" - [3.6. `.framework-config` framework configuration file](#36-framework-config-framework-configuration-file)\n"
"- [4. FrameworkLint](#4-frameworklint)\n"
"- [5. Best practices](#5-best-practices)\n"
"- [6. Acknowledgements](#6-acknowledgements)")
subst = "$1$4"
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