简体   繁体   English

正则表达式与特殊字符?

[英]regex with special characters?

i am looking for a regex that can contain special chracters like / \\ . ' " 我正在寻找一个可以包含/ \\ . ' "等特殊字符的正则表达式/ \\ . ' " / \\ . ' "

in short i would like a regex that can match the following: 总之,我想要一个可以匹配以下内容的正则表达式:

  • may contain lowercase 可能包含小写
  • may contain uppercase 可能包含大写
  • may contain a number 可能包含一个数字
  • may contain space 可能包含空间
  • may contain / \\ . ' " 可能包含/ \\ . ' " / \\ . ' "

i am making a php script to check if a certain string have the above or not, like a validation check. 我正在制作一个PHP脚本来检查某个字符串是否具有上述内容,如验证检查。

The regular expression you are looking for is 你正在寻找的正则表达式是

^[a-z A-Z0-9\/\\.'"]+$

Remember if you are using PHP you need to use \\ to escape the backslashes and the quotation mark you use to encapsulate the string. 请记住,如果您使用的是PHP,则需要使用\\来转义反斜杠和用于封装字符串的引号。

In PHP using preg_match it should look like this: 在使用preg_match的PHP中,它应该如下所示:

preg_match("/^[a-z A-Z0-9\\/\\\\.'\"]+$/",$value);

This is a good place to find the regular expressions you might want to use. 这是查找您可能想要使用的正则表达式的好地方。 http://regexpal.com/ http://regexpal.com/

您总是可以通过在特殊字符前添加\\来转义它们。

尝试这个:

preg_match("/[A-Za-z0-9\/\\.'\"]/", ...)

NikoRoberts is 100% correct. NikoRoberts 100%正确。 I would only add the following suggestion: When creating a PHP regex pattern string, always use: single-quotes . 我只会添加以下建议:创建PHP正则表达式模式字符串时,请始终使用: 单引号 There are far fewer chars which need to be escaped (ie only the single quote and the backslash itself needs to be escaped (and the backslash only needs to be escaped if it appears at the end of the string)). 需要转义的字符要少得多(即只有单引号和反斜杠本身需要转义(如果字符串的末尾出现反斜杠,则只需转义))。

When dealing with backslash soup , it helps to print out the (interpreted) regex string. 处理反斜杠汤时 ,有助于打印出(解释的)正则表达式字符串。 This shows you exactly what is being presented to the regex engine. 这显示了正确呈现给正则表达式引擎的内容。

Also, a "number" might have an optional sign? 另外,“数字”可能有一个可选的标志? Yes? 是? Here is my solution (in the form of a tested script): 这是我的解决方案(以经过测试的脚本的形式):

<?php // test.php 20110311_1400
    $data_good = 'abcdefghijklmnopqrstuvwxyzABCDE'.
        'FGHIJKLMNOPQRSTUVWXYZ0123456789+- /\\.\'"';
    $data_bad = 'abcABC012~!@##$%^&*()';

    $re = '%^[a-zA-Z0-9+\- /\\\\.\'"]*$%';
    echo($re ."\n");
    if (preg_match($re, $data_good)) {
        echo("CORRECT: Good data matches.\n");
    } else {
        echo("ERROR! Good data does NOT match.\n");
    }
    if (preg_match($re, $data_bad)) {
        echo("ERROR! Bad data matches.\n");
    } else {
        echo("CORRECT: Bad data does NOT match.\n");
    }
?>

The following regex will match a single character that fits the description you gave: 以下正则表达式将匹配符合您给出的描述的单个字符:

[a-zA-Z0-9\ \\\/\.\'\"]

If your point is to insure that ONLY characters in this range of characters are used in your string, then you can use the negation of this which would be: 如果您要确保在字符串中仅使用此字符范围内的字符,那么您可以使用以下字符来否定:

[^a-zA-Z0-9\ \\\/\.\'\"]

In the second case, you could use your regex to find the bad stuff (that you don't want to be included), and if it didn't find anything then your string pattern must be kosher, because I'm assuming that if you find one character that is not in the proper range, then your string is not valid. 在第二种情况下,你可以使用你的正则表达式来找到坏东西(你不想被包含在内),如果它没有找到任何东西,那么你的字符串模式必须是犹太教,因为我假设如果如果找到一个不在适当范围内的字符,那么你的字符串无效。

so to put it in PHP syntax: 所以把它放在PHP语法中:

$regex = "[^a-zA-Z0-9\ \\\/\.\'\"]"

if preg_match( $regex, ... ) {
    // handle the bad stuff
}

Edit 1: I've completely ignored the fact that backslashes are special in php double-quoted strings, so here is a correcting to the above code: 编辑1:我完全忽略了反斜杠在php双引号字符串中特殊的事实,所以这里是对上述代码的修正:

$regex = "[^a-zA-Z0-9\\ \\\\\\/\\.\\'\\\"]"

If that doesn't work it shouldn't take too much for someone to debug how many of the backslashes need to be escaped with a backslash, and what other characters need also to be escaped.... 如果这不起作用,那么有人不应该花太多时间来调试需要使用反斜杠转义的反斜杠数量,以及其他角色也需要转义....

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

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