php 正则表达式除了空格之外的任何字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6808310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:19:14  来源:igfitidea点击:

Regular expression any character but a white space

phpregexwhitespace

提问by Wen

I'm creating a password validator which takes any character but whitespaces and with at least 6 characters.

我正在创建一个密码验证器,它接受除空格之外的任何字符,并且至少有 6 个字符。

After searching the best I came up is this is this example: What is the regular expression for matching that contains no white space in between text?

在搜索了最好的之后,我想出了这个例子: 什么是匹配的正则表达式,文本之间不包含空格?

It disallows any spaces inbetween but does allow starting and ending with a space. I want to disallow any space in the string passed.

它不允许中间有任何空格,但允许以空格开头和结尾。我想禁止传递的字符串中有任何空格。

I tried this but it doesn't work:

我试过这个,但它不起作用:

if (preg_match("/^[^\s]+[\S+][^\s]{6}$/", $string)) {
  return true;
} else {
  return false;
}

Thanks.

谢谢。

回答by mpen

The simplest expression:

最简单的表达:

^\S{6,}$

^means the start of the string
\Smatches any non-whitespace character
{6,}means 6 or more
$means the end of the string

^表示字符串的开头
\S匹配任何非空白字符
{6,}表示 6 或更多
$表示字符串的结尾

In PHP, that would look like

在 PHP 中,这看起来像

preg_match('/^\S{6,}$/', $string)

Edit:

编辑:

>> preg_match('/^\S{6,}$/', "abcdef\n")
1
>> preg_match('/^\S{6,}\z/', "abcdef\n")
0
>> preg_match('/^\S{6,}$/D', "abcdef\n")
0

Qtaxis right. Good call! Although if you're taking input from an HTML <input type="text">you probably won't have any newlines in it.

Qtax是对的。好决定!虽然如果您从 HTML 获取输入,<input type="text">您可能不会有任何换行符。

回答by Qtax

Something like this:

像这样的东西:

/^\S{6,}\z/

Can be quoted like:

可以引用如下:

preg_match('/^\S{6,}\z/', $string)

All answers using $are wrong(at least without any special flags). You should use \zinstead of $if you do not want to allow a line break at the end of the string.

所有使用$的答案都是错误的(至少没有任何特殊标志)。如果您不想在字符串末尾允许换行,则应使用\z代替$

  • $matches end of string or beforea line break at end of string(if no modifiers are used)
  • \zmatches end of string (independent of multiline mode)
  • $匹配字符串末尾或字符串末尾的换行符之前(如果未使用修饰符)
  • \z匹配字符串的结尾(独立于多行模式)

From http://www.pcre.org/pcre.txt:

http://www.pcre.org/pcre.txt

 ^           start of subject
              also after internal newline in multiline mode
 \A          start of subject
 $           end of subject
              also before newline at end of subject
              also before internal newline in multiline mode
 \Z          end of subject
              also before newline at end of subject
 \z          end of subject

回答by Igoris Azanovas

\S- Matches any non-white-space character. Equivalent to the Unicode character categories [^\f\n\r\t\v\x85\p{Z}]. If ECMAScript-compliant behavior is specified with the ECMAScript option, \Sis equivalent to [^ \f\n\r\t\v].

\S- 匹配任何非空白字符。相当于 Unicode 字符类[^\f\n\r\t\v\x85\p{Z}]。如果使用 ECMAScript 选项指定了符合 ECMAScript 的行为,\S则等效于[^ \f\n\r\t\v].

The start of string you can do : ^[ \t]+, and for end : [ \t]+$(tab and spaces)

您可以执行的字符串开头 :^[ \t]+和结尾 : [ \t]+$(制表符和空格)

ETA:

预计到达时间:

By the way, you regex [\S+], i think you're looking for : [\S]+

顺便说一句,你正则表达式[\S+],我想你正在寻找:[\S]+

回答by Ryan Gross

I think you should be fine using the following, which would match any string longer than 1 character with no whitespace:

我认为您应该可以使用以下内容,它可以匹配任何长度超过 1 个字符且没有空格的字符串:

^[^\s]+$

You can see the test here: http://regexr.com?2ua2e.

您可以在此处查看测试:http: //regexr.com?2ua2e

回答by Bueller

Try this. This will match at least 6 non whitespace characters followed by any number of additional non whitespace characters.

尝试这个。这将匹配至少 6 个非空白字符,后跟任意数量的其他非空白字符。

^[^\s]{6}[^\s]*$