正则表达式,匹配标签之间的换行符 [英] Regex, match newlines between tags

查看:95
本文介绍了正则表达式,匹配标签之间的换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 PHP 中有这个正则表达式:

I have this regex in PHP:

preg_match('/\[summary\](.+)\[\/summary\]/i', $data['text'], $match);

当摘要标签之间的文本在一行上时,它工作正常.但是,当它包含换行符时,它不匹配.

It works fine when the text between the summary tags is on one line. However, when it contains newlines, it doesn't match.

我试图在这里找到正确的修饰符:http://nl2.php.net/manual/en/reference.pcre.pattern.modifiers.php但是与换行符相关的唯一一个是m",这不是我想要的.

I've tried to find a correct modifier here: http://nl2.php.net/manual/en/reference.pcre.pattern.modifiers.php But the only one related to newlines is "m" and that doesn't do what I want.

如何进行这项工作?

推荐答案

您链接到的手册页描述了另一个影响换行处理方式的选项.

The man page you've linked to describes another options that has an effect on how line breaks are handled.

s (PCRE_DOTALL)如果设置了此修饰符,则模式中的点元字符匹配所有字符,包括换行符.没有它,换行符被排除在外.此修饰符等效于 Perl 的/s 修饰符.否定类如 [^a] 始终匹配换行符,与此修饰符的设置无关.
s (PCRE_DOTALL) If this modifier is set, a dot metacharacter in the pattern matches all characters, including newlines. Without it, newlines are excluded. This modifier is equivalent to Perl's /s modifier. A negative class such as [^a] always matches a newline character, independent of the setting of this modifier.

这篇关于正则表达式,匹配标签之间的换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆