$re = '/^(\s*- \[([0-9]+\.)+ [^]]+\]\(#)([^)]+)\)$/m';
$str = '- [1. Why ?](#1-why-)
- [2. Compile tool](#2-compile-tool)
- [3. compile command help](#3--compile-command-help)
- [3.1. .framework-config environment variables](#31-framework-config-environment-variables)
- [3.2. Template variables](#32-template-variables)
- [3.3. Bash-tpl templating](#33-bash-tpl-templating)
- [3.4. Bash-tpl macros](#34-bash-tpl-macros)
- [3.4.1. dynamicTemplateDir](#341-dynamictemplatedir)
- [3.4.2. dynamicSrcFile](#342-dynamicsrcfile)
- [3.4.3. dynamicSrcDir](#343-dynamicsrcdir)
- [3.5. directives and template](#35-directives-and-template)
- [3.5.1. `# FUNCTIONS` directive](#351--functions-directive)
- [3.5.2. `VAR_*` directive (optional)](#352-var_-directive-optional)
- [3.5.3. `BIN_FILE` directive (optional)](#353-bin_file-directive-optional)
- [3.5.5. `EMBED` directive (optional)](#355-embed-directive-optional)
- [3.5.4. Compiler - Embed::embed](#354-compiler---embedembed)
- [3.6. `.framework-config` framework configuration file](#36-framework-config-framework-configuration-file)
- [4. FrameworkLint](#4-frameworklint)
- [5. Best practices](#5-best-practices)
- [6. Acknowledgements](#6-acknowledgements)';
$subst = "$1$4";
$result = preg_replace($re, $subst, $str);
echo "The result of the substitution is ".$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 PHP, please visit: http://php.net/manual/en/ref.pcre.php