views:

152

answers:

4

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

A: 
$pat = "/^[\w. ,\\/:_()'\"-]/";
pix0r
doesn't work, passes all values from my updated example
Phill Pafford
A: 

To match all those, you just need:

preg_match("/[a-zA-Z0-9,.'\"()_- :]/", $string);
Should escape the - :) It will give error this way, or not work correctly (possibly this one)
bisko
I have a match almost exactly like this and I don't escape hyphens, works fine :)
doesn't work, failed all values in my updated example
Phill Pafford
A: 
/^[-a-zA-Z0-9,.'"()_: ]+$/

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

bisko
doesn't work, Failed all values in my updated example
Phill Pafford
yes I did the escape as well: "/^[-a-zA-Z0-9,.'\"()_: ]+$/"
Phill Pafford
A: 

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

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

Thanks to all for the help

Phill Pafford