Q: Preg_match in php not working properly when i tried it I'm trying to get it to echo "match", but it keep echoing "not match". What am I doing wrong?    
<?php

$string = "anyway, why is it not matching?";
if (preg_match("~^([a-z]+ |[,.!?] ?)?(who|whom|what|where|when|why|how|which)[s]?\b~i", $string)) {
echo "match";
}
else { echo "not match"; }

?>


A: The start of your regex ^([a-z]+ |[,.!?] ?) denotes that the string must start with any number of letters followed by a space, or exactly one punctuation mark followed by a space.
What you want is to match one to one words followed by zero to one punctuation characters followed by zero to one spaces:
^([a-z]+[,.!?]? ?)?
~^([a-z]+[,.!?]? ?)?(who|whom|what|where|when|why|how|which)[s]?\b~i

This will match the comma and space in the first group, and why in the second group, as can be seen broken ndown on Regexer here and working on 3v4l here.
