简体   繁体   English

如何在此正则表达式中允许空格?

[英]How do I allow spaces in this regex?

我是regex的业余爱好者,如何在此regex中允许空格(无所谓)?

if(preg_match('/[^A-Za-z0-9_-]/', $str)) return FALSE;
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 ). 假设您所说的“允许”是:该正则表达式用于验证字符串,如果匹配 ,则不允许使用该字符串(因此return FALSE )。 So the characters in the negated character class ( [^...] ) are actually the allowed characters. 因此,否定字符类( [^...] )中的字符实际上是允许的字符。 (This is causing some general confusion in this question.) (这在这个问题上引起了一些普遍的困惑。)

the \\s in the regular expretion like this '/[^A-Za-z0-9_-\\s]/' 像这样的正则表达式中的\\s '/[^A-Za-z0-9_-\\s]/'
mean the space 意味着空间

Not so much an answer to your question, but a site I find useful for checking regex expressions . 它不是您问题的答案,而是一个我发现对检查regex表达式有用的站点。 It also explains what each part of the expression does / means as you hover over it in the input field. 当您将其悬停在输入字段中时,它还会解释表达式的每个部分的作用/含义。

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_- . 如果$str包含不是A-Za-z0-9_-任何字符,则正则表达式将成功A-Za-z0-9_- Since a space is not one of these characters, the regular expression will match, and the whole statement returns FALSE . 由于空格不是这些字符之一,因此正则表达式将匹配,并且整个语句返回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). 如果这不是您想要的,并且如果$str具有不在A-Za-z0-9_-或空格中的任何字符,则希望您的正则表达式匹配,那么您需要将其更改为A-Za-z0-9_ - (请注意下划线和连字符之间的空格)。 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 . 因此,当字符串的字符不是A-Za-z0-9_ - ,正则表达式将匹配,并且您的语句将返回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. 如果您的字符串完全由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 . 编辑:这是一个示例:如果您的字符串是abc123def ,则当前正则表达式将不匹配,并且您将不会返回FALSE If your string is abc123 def , the regular expression will match and the statement will return FALSE . 如果您的字符串是abc123 def ,则正则表达式将匹配,并且该语句将返回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 . 如果将字符类更改为A-Za-z0-9_ - ,则正则表达式将无法同时匹配abc123defabc123 def ,并且不会返回FALSE

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_ \\-] /

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 不区分大小写的标志i并省略字母范围之一
  • and write [0-9] as \\d 并将[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代表[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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM