我有一根很大的绳子。这个字符串中有许多段落。每个段落都以标题开始,并遵循特定模式。
示例:
== Title1 == // Paragraph starts
.............
............. // Some texts
.............
End of Paragraph
===Title2 === // Paragraph starts
.............
............. // Some texts
.............
标题样式为:
1.) New Paragraph title starts with an equal to ( = ) and can be followed by any number of =.
2.) After = , there can be a white space ( not necessary though ) and it is followed by text.
3.) After text completion, again there can be a white space ( not necessary ), followed by again any number of equal to's ( = ).
4.) Now the paragraph starts. I have to extract the text until it encounters a similar pattern.
有人能帮我怎么用regex吗?短暂性脑缺血发作
你可以用
参见regex demo
细节
(?m)^
-行首=+
-1个或更多=
字符[^\S\r\n]*
-除CR和LF之外的零个或多个空格字符(.*?)
-第1组:除换行符以外的任何零个或多个字符,尽可能少[^\S\r\n]*
-除CR和LF之外的零个或多个空格字符=+
-1个或更多=
字符\s*
-0+空格(.*(?:\r?\n(?!==+.*?=).*)*)
-第2组:.*
-尽可能多的零个或多个字符,而不是换行符(?:\r?\n(?!=+.*?=).*)*
-零个或多个\r?\n(?!=+.*?=)
-一个可选的CR和LF,后面不跟1+=
s,然后是除换行符以外的任何字符,尽可能少,然后是1+=
s.*
-尽可能多的零个或多个字符,而不是换行符Python demo:
输出:
也许这有助于找到每个段落的标题和每一段的行。你知道吗
你可以试试这个-
会给你
Title1
。或者假设你想要列表中所有匹配的标题,你可以-图案是-
之后,我们可以通过将
=
替换为''
并将其从空格中剥离来提取文本。您可以在regex101尝试,这在测试regex时非常有用相关问题 更多 >
编程相关推荐