简体   繁体   English

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

[英]Regex to match everything between hash tags, multiline (PHP)

I'm trying to make a wiki markdown style parser where I have headers defined by hashtags. 我正在尝试制作一个Wiki markdown样式解析器,其中具有由标签定义的标头。 Eg: 例如:

# Heading 1

Some information here below the heading

Another paragraph of information

## Subheading 1

Paragraph related to the subheading

What I'm trying to do is capturing everything between one hash tag and another hashtag (as well as the hash tags themselves) 我正在尝试捕获一个哈希标签和另一个哈希标签之间的所有内容(以及哈希标签本身)

So in the above, I would want to capture 所以在上面,我想捕捉一下

 Heading 1 (and #)

 Some information here below the heading

 Another paragraph of information

And... 和...

 Subheading 1 (and ##)

 Paragraph related to the subheading

So far I have /(#+) ?(.+)/ and that works perfectly for content on the same line after a hash tag, but not for any text separated by newlines. 到目前为止,我有/(#+) ?(.+)/和完全适用于哈希标签后,在同一行的内容,而不是通过换行分隔的任何文本。

However, I can't seem to match and newlines in the (.+) part of the regex pattern, without also gobbling up all of the new lines that start with hash tags. 但是,我似乎无法在正则表达式模式的(.+)部分中匹配换行符,而又不去掉以哈希标签开头的所有新行。

I'm sure it's something simple. 我敢肯定这很简单。

You can use this negation based regex: 您可以使用以下基于否定的正则表达式:

# *([^#]+)

RegEx Demo 正则演示

Code: 码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM