简体   繁体   中英

php preg_match regex

Still having RegEx issues.. Need to match the following characters

a-zA-z9-0 , . ' " ( ) _ - : (SPACE)

Not all the values will have all these but could have them. I have everything withing but the parentheses, Single and Double Quoytes

/^[\w. ,\/:_-]+$/ 

UPDATE:

I got it working with this: "/^[\\w. ,:()'\\"-]+$/"

$val_1 = "Abh acb 123 . - _ 's ";
$val_2 = "Asc";
$val_3 = "234";
$val_4 = "nj%"; // Fail
$val_5 = "Help (me)";
$val_6 = "What's wrong?"; // Fail
$val_7 = "She's here";
$val_8 = "No: 123.00, 432.00";
$val_9 = 'Need to " Double" ';

$var_array = array($val_1, $val_2, $val_3, $val_4, $val_5, $val_6, $val_7, $val_8, $val_9);

foreach ($var_array as $k=>$d) {
    if ((preg_match("/^[\w. ,:()'\"-]+$/", $d))) {
        echo "Yeah it matches!!!<span style='color:green'>".$d."</span><br />";
    } else {
        echo "Try again, thie FAILED<span style='color:red'>".$d."</span><br />";
    }
}

Thanks for all for helping out

$pat = "/^[\w. ,\\/:_()'\"-]/";

要匹配所有这些,您只需要:

preg_match("/[a-zA-Z0-9,.'\"()_- :]/", $string);
/^[-a-zA-Z0-9,.'"()_: ]+$/ 

This should work. But if you put it into a string be sure to escape the needed quotes.

With the help of the other submitting I have found the solution that works:

"/^[\w. ,:()'\"-]+$/"

Thanks to all for the help

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