当标题包含
[[Wikilink]]
关键字时,我尝试匹配特定标题级别下的内容(用例:黑曜石)
我希望它匹配较低级别的标题。
示例
[[Wikilink]]
位于H2中,然后匹配下面的所有内容,直到h2或更高版本或文件末尾
难度:
[[Wikilink]]
H级未知。正则表达式应该能够解析多个不一致的文件,其中 [[Wikilink]]
可以是 H1、H2、H3 等。
我当前的正则表达式在遇到任何标题时都会失败:
(^#+ )[^\[]*?\[\[Wikilink]\][^\n]*?\n([\S\s]*?)(?1)
沙箱:https://regex101.com/r/bLdifP/1
不知何故与这个问题有关:正则表达式匹配降价标题和嵌套在特定标题下的文本
尝试以下正则表达式。
^(#+) .*\[\[Wikilink\]\].*$(?=([\S\s]*?)(?:^(?!\1#+)#+ |\z))
正则表达式可以分解如下。
^ the beginning of a "line"
--------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------
#+ '#' (1 or more times (matching the most
amount possible))
--------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------
' '
--------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------
\[ '['
--------------------------------------------------------------------
\[ '['
--------------------------------------------------------------------
Wikilink 'Wikilink'
--------------------------------------------------------------------
\] ']'
--------------------------------------------------------------------
\] ']'
--------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------
$ before an optional \n, and the end of a
"line"
--------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------
( group and capture to \2:
--------------------------------------------------------------------
[\S\s]*? any character of: non-whitespace (all
but \n, \r, \t, \f, and " "),
whitespace (\n, \r, \t, \f, and " ")
(0 or more times (matching the least
amount possible))
--------------------------------------------------------------------
) end of \2
--------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------
^ the beginning of a "line"
--------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------
\1 what was matched by capture \1
--------------------------------------------------------------------
#+ '#' (1 or more times (matching the
most amount possible))
--------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------
#+ '#' (1 or more times (matching the
most amount possible))
--------------------------------------------------------------------
' '
--------------------------------------------------------------------
| OR
--------------------------------------------------------------------
\z the end of the string
--------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------
) end of look-ahead