I have these strings. I want a regular expression to match them and return true when I pass them to preg_match function.
do you want to eat katak at my home
Use a quantifier:
$pattern = "/do you want to eat ([a-z0-9]*) at my hometown\?/i";
// here __^
and escape the ?
==> \?
$text = "do you want to eat meatball at my hometown?";
$pattern = "/(\w+)(?=\sat)/";
if (preg_match($pattern, $text))
(\w+)
matches one or more word characters.
(?=\sat)
is a positive lookahead that matches one whitespace \s
and the letters at
.
Regex live demo