用正则表达式检测非空字符串? [英] Detect non-empty string with regexp?

查看:564
本文介绍了用正则表达式检测非空字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写检测字符串非空的 PHP 正则表达式?

How do I write a PHP regexp which detects that a string is not empty?

空意味着,在这种情况下是 0 个字符.例如,单个空格或单个换行符算作非空.

Empty means, in this case 0 characters. A single space, or a single newline counts as not empty, for instance.

(它必须是适合 preg_match() 的正则表达式,因为我有一个包含各种正则表达式的查找表,并且不想以任何特殊的方式处理这种情况,如果在这里不使用正则表达式会使代码复杂化.)

(It has to be regexp suitable for preg_match(), since I have a lookup table with various regexps and don't want to handle this case in any special way, it would complicate the code to not use a regexp here.)

更新:

出于可悲的原因,我也不能在 // 之外使用任何正则表达式修饰符,例如s".

I also can not use any regex modifiers such as "s" outside the // for sad reasons.

致反对者:问题是不是太简单了?还是不够清楚?

推荐答案

/[\s\S]/

匹配任何字符,即使您不能使用 /s 修饰符.

matches any character, even if you can't use the /s modifier.

您不需要量词(+),因为如果一个字符匹配,则条件已经满足.

You don't need a quantifier (+) because if one character matches, then the condition is already fulfilled.

这篇关于用正则表达式检测非空字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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