PHP Regex任何字符 [英] PHP Regex Any Character

查看:64
本文介绍了PHP Regex任何字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

php正则表达式中的.字符接受所有字符,除了换行符.我可以用来接受所有字符,包括换行符吗?

The . character in a php regex accepts all characters, except a newline. What can I use to accept ALL characters, including newlines?

推荐答案

这通常用于捕获所有字符:

This is commonly used to capture all characters:

[\s\S]

您可以通过相同的方式使用"Type-X + Non-Type-X"的任何其他组合:

You could use any other combination of "Type-X + Non-Type-X" in the same way:

[\d\D]
[\w\W]

但是[\s\S]被惯例认为是真正的一切"的简写.

but [\s\S] is recognized by convention as a shorthand for "really anything".

如果通过"s"修饰符将正则表达式切换为"dotall"(也称为单行")模式,则也可以使用..有时,这不是可行的解决方案(例如,黑盒中的动态正则表达式,或者如果您不想修改 entire 正则表达式).在这种情况下,无论正则表达式如何配置,其他替代方法都一样.

You can also use the . if you switch the regex into "dotall" (a.k.a. "single-line") mode via the "s" modifier. Sometimes that's not a viable solution (dynamic regex in a black box, for example, or if you don't want to modify the entire regex). In such cases the other alternatives do the same, no matter how the regex is configured.

这篇关于PHP Regex任何字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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