php 如何在此正则表达式中允许空格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4798095/
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
How do I allow spaces in this regex?
提问by soren.qvist
I'm such an amateur at regex, how do I allow spaces(doesn't matter how many) in this regex?
我是 regex 的业余爱好者,我如何在这个 regex 中允许空格(不管有多少)?
if(preg_match('/[^A-Za-z0-9_-]/', $str)) return FALSE;
回答by chaos
if(preg_match('/[^A-Za-z0-9_ -]/', $str)) return FALSE;
Note that I put the space before the hyphen. If the space were after the hyphen, I would be specifying a character range from underscore to space. (Issue also evadable by putting a backslash before the hyphen to escape it.)
请注意,我将空格放在连字符之前。如果空格在连字符之后,我将指定从下划线到空格的字符范围。(问题也可以通过在连字符前放置反斜杠来逃避它。)
This is assuming that what you mean by "allow" is: this regex is being used to validate a character string, and if it matches, then the character string is disallowed(hence return FALSE
). So the characters in the negated character class ([^...]
) are actually the allowedcharacters. (This is causing some general confusion in this question.)
这是假设您所说的“允许”是指:此正则表达式用于验证字符串,如果匹配,则不允许使用该字符串(因此return FALSE
)。所以否定字符类 ( [^...]
) 中的字符实际上是允许的字符。(这在这个问题中引起了一些普遍的混淆。)
回答by AmirModiri
the \s
in the regular expretion like this '/[^A-Za-z0-9_-\s]/'
mean the space
将\s
在常规expretion这样'/[^A-Za-z0-9_-\s]/'
意味着空间
回答by Scoobler
Not so much an answer to your question, but a site I find useful for checking regex expressions. It also explains what each part of the expression does / means as you hover over it in the input field.
与其说是对您问题的回答,不如说是一个我发现对检查正则表达式很有用的网站。它还解释了当您将鼠标悬停在输入字段中时表达式的每个部分的作用/含义。
回答by CanSpice
Your question is unclear. The regular expression, as it stands, will succeed if $str
has any character in it that is not A-Za-z0-9_-
. Since a space is not one of these characters, the regular expression will match, and the whole statement returns FALSE
.
你的问题不清楚。正则表达式,就目前而言,如果其中$str
包含不是A-Za-z0-9_-
. 由于空格不是这些字符之一,正则表达式将匹配,并且整个语句返回FALSE
。
If this isn't what you want, and you want your regular expression to match if $str
has any character that is not in A-Za-z0-9_-
or a space, then you need to change it to A-Za-z0-9_ -
(note the space between the underscore and the hyphen). Thus when your string has a character that is not A-Za-z0-9_ -
, the regular expression will match, and your statement will return FALSE
. If your string is made up entirely of A-Za-z0-9_ -
, then the regular expression will fail to match, and your processing will continue to the next line.
如果这不是您想要的,并且您希望正则表达式匹配$str
任何不在A-Za-z0-9_-
或空格中的字符,那么您需要将其更改为A-Za-z0-9_ -
(注意下划线和连字符之间的空格)。因此,当您的字符串中有一个不是 的字符时,正A-Za-z0-9_ -
则表达式将匹配,并且您的语句将返回FALSE
。如果您的字符串完全由 组成A-Za-z0-9_ -
,则正则表达式将无法匹配,您的处理将继续到下一行。
Edit:Here's an example: If your string is abc123def
, currently the regular expression will not match and you will not return FALSE
. If your string is abc123 def
, the regular expression will match and the statement will return FALSE
. If you change the character class to A-Za-z0-9_ -
, then the regular expression will fail to match for both abc123def
and abc123 def
, and you will not return FALSE
.
编辑:这是一个示例:如果您的字符串是abc123def
,则当前正则表达式将不匹配,您将不会返回FALSE
。如果您的字符串是abc123 def
,则正则表达式将匹配并且语句将返回FALSE
。如果您将字符类更改为A-Za-z0-9_ -
,则正则表达式将无法同时匹配abc123def
和abc123 def
,您将不会返回FALSE
。
回答by Olegas
If you need to ALLOW only space you'll need '/ /'
如果您只需要允许空间,您将需要 '//'
If you need to ALLOW any whitespace char (space, tab, newline) - use '/\s/'
如果您需要允许任何空白字符(空格、制表符、换行符) - 使用 '/\s/'
And if you need to add a space to your pattern (means to ignore space) - use /[^A-Za-z0-9_\ -]/
如果你需要在你的模式中添加一个空格(意味着忽略空格) - 使用 /[^A-Za-z0-9_\ -]/
回答by mickmackusa
You only need to add a literal blank space in your negated character class.
您只需要在否定字符类中添加一个文字空格。
It would be more concise to use:
使用会更简洁:
- the case-insensitive flag
i
and omit one of the letter ranges - and write
[0-9]
as\d
- 不区分大小写的标志
i
并省略字母范围之一 - 并写
[0-9]
为\d
like this:
像这样:
if (preg_match('/[^A-Z0-9_ -]/i', $str)) return FALSE;
but even better would be to use \w
which represents [A-Za-z0-9_] (which is most of your pattern) to write:
但更好的是使用\w
which 代表 [A-Za-z0-9_] (这是你的大部分模式)来写:
if (preg_match('/[^\w -]/', $str)) return FALSE;
This pattern states that if $str
contains any character not in [A-Za-z0-9_ -]
then false
will be returned.
这种模式指出,如果$str
包含任何字符不[A-Za-z0-9_ -]
那么false
将被退回。