正则表达式匹配自定义降价语法

时间:2016-08-23 04:39:18

标签: regex

我想将以下内容与多个捕获组匹配:

Definition 1
: This is the definition text that described the term. Can have markdown formatting and
multiple lines.
Definition 2
: This is the definition text with **markdown**.`code`

我还想用以下文本(HTML定义列表)替换它:

<dl>
    <dt>Definition 1</dt>
    <dd>This is the definition text that described the term. Can have markdown formatting and
multiple lines.</dd>
    <dt>Definition 1</dt>
    <dd>This is the definition text with **markdown**.`code`</dd>
</dl>

1 个答案:

答案 0 :(得分:1)

您可以分两步完成此操作:

1。插入dtdd代码

使用以下方式执行搜索:

(.*)\R: ((?:.+(?:\R|$))*?)(?=\R|.*\R:|$)/g

并替换为:

<dt>$1</dt>\n<dd>$2</dd>\n

请参阅regex tester

2。添加dl代码

使用上一次替换的结果并执行以下搜索:

/(<dt>.*?<\/dd>(?!\s*<dt>))/gs

并替换为:

<dl>\n$1\n</dl>

请参阅regex tester

说明

如果不支持\R转义,请改用\n 可能需要将后引用$1$2更改为\1\2,具体取决于您的正则表达式引擎。