Regular expression with special characters?

I am looking for a regex that might contain special trainers like / \ . ' "

In short, I would like a regex that can match the following:

  • may contain lowercase letters
  • may contain capital letters
  • may contain a number
  • may contain a space
  • may contain / \ . ' "

I am doing a php script to check if a particular line has a higher or not, e.g. check validation.

+3
source share
5 answers

The regular expression you are looking for

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

Remember that if you use PHP, you need to use \ to avoid backslashes and quotes that you use to encapsulate strings.

In PHP using preg_match it should look like this:

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

, , , . http://regexpal.com/

+6

, \ .

+2

:

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

NikoRoberts 100% . : PHP : . , (.. ( , )).

, () . , .

, "" ? ? ( 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");
    }
?>
+2

regex , , :

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

, , :

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

( ), , , , , , .

, PHP:

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

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

1: , php, :

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

If this does not work, it does not take too much for someone to debug how many backslashes need to be flushed with a backslash, and also that other characters must also be escaped ....

+1
source

All Articles