简体   繁体   中英

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.

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.

In PHP using preg_match it should look like this:

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/

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

尝试这个:

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

NikoRoberts is 100% correct. I would only add the following suggestion: When creating a PHP regex pattern string, always use: single-quotes . 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:

$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:

$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....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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