正则表达式匹配文本块直到第一个双换行符?
我正在制作一个简单的 Textile 解析器,并尝试为“blockquote”编写正则表达式,但在匹配多个新行时遇到困难。示例:
bq. first line of quote second line of quote third line of quote not part of the quote
它将通过 preg_replace()
替换为块引用标记,因此基本上它需要匹配 "bq."
和它遇到的第一个双新行之间的所有内容。我能做到的最好的办法就是获得报价的第一行。谢谢
I'm making a simple Textile parser and am trying to write a regular expression for "blockquote" but am having difficulty matching multiple new lines. Example:
bq. first line of quote second line of quote third line of quote not part of the quote
It will be replaced with blockquote tags via preg_replace()
so basically it needs to match everything between "bq."
and the first double new line it comes across. The best I can manage is to get the first line of the quote. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
试试这个正则表达式:
含义:
\r?\n
匹配 Windows、*nix 和(较新的)MacOS 换行符。如果您需要考虑真正的旧 Mac 计算机,请向其中添加单个\r
:\r?\n|\r
Try this regex:
meaning:
The
\r?\n
matches a Windows, *nix and (newer) MacOS line breaks. If you need to account for real old Mac computers, add the single\r
to it:\r?\n|\r
这个接受的答案只为我捕获了该块的最后一个字符。我最终使用了这个:
This accepted answer only captured the last character of the block for me. I ended up using this:
这行得通吗?
我相信 's' 代表单行。
Would this work?
I believe 's' stands for single line.
编辑:呃,误读了这个问题..“bq”。意义重大。
有时通过网络表单输入的数据包含 \r\n 而不是仅 \n 这会使其
问号使其在找到第一个双返回后添加结束块引号(“非贪婪”我相信它被称为),所以任何其他双倍回报单独保留(如果这不是你想要的,显然将其取出)。
Edit: Ehr, misread the question.. "bq." was significant.
Sometimes data that is entered via webforms contains \r\n instead of just \n which would make it
The questionmark makes it add the closing blockquotes after the first double return found ("non-greedy" I believe it's called), so any other double returns are left alone (if that is not what you want, take it out obviously).
我的直觉告诉我类似的事情...
就像上面的人说的那样,正则表达式末尾的
/
后面的s
标志意味着.< /code> 将匹配新行字符。通常,如果没有这个,正则表达式就是一种单行的东西。
那么
.+
后面的问号?
表示非贪婪匹配,这样.+
就不会尽可能匹配;相反,它将匹配尽可能小的值,以便\n\n
将匹配第一个可用的双行。您计划在多大程度上支持 Textile 的功能?因为您的正则表达式可能会变得非常复杂,因为 Textile 允许诸如...
或...之
类的事情,我认为您的正则表达式替换技术将无法处理所有这些事情。
My instincts tell me something like...
Just like the above fella says, the
s
flag after the/
at the end of the RegEx means that the.
will match new line characters. Usually, without this, RegExs are kind of a one line thing.Then the question mark
?
after the.+
denotes a non-greedy match so that the.+
won't match as it can; instead it will match the minimum possible, so that the\n\n
will match the first available double line.To what extent are you planning on supporting features of Textile? Because your RegEx can get pretty complicated, as Textile allows things like...
or...
All of which your regex-replace technique won't be able to handle, methinks.