正则表达式匹配哈希标记之间的所有内容,多行(PHP)

时间:2014-11-11 17:57:14

标签: php regex

我试图制作一个wiki markdown样式解析器,其中我有由hashtags定义的头文件。 E.g:

# Heading 1

Some information here below the heading

Another paragraph of information

## Subheading 1

Paragraph related to the subheading

我尝试做的是捕获一个哈希标记和另一个哈希标记之间的所有内容(以及哈希标记本身)

所以在上面,我想要捕获

 Heading 1 (and #)

 Some information here below the heading

 Another paragraph of information

和...

 Subheading 1 (and ##)

 Paragraph related to the subheading

到目前为止,我有/(#+) ?(.+)/,它适用于散列标记后同一行的内容,但不适用于换行符分隔的任何文本。

但是,我似乎无法在正则表达式模式的(.+)部分匹配和换行,也不会吞噬所有以哈希标记开头的新行。

我确定它很简单。

1 个答案:

答案 0 :(得分:2)

您可以使用此基于否定的正则表达式:

# *([^#]+)

RegEx Demo

<强>代码:

if ( preg_match_all('/# *([^#]+)/', $input, $matches) )
   print_r($matches[1]);
相关问题