I am trying to find a php preg_match that can match
test1 test2[…] but not test1 test2 […]
and return test2(…) as the output as $match.
I tried
preg_match('/^[a-zA-Z0-9][\[](.*)[\]]$/i',"test1 test2[...]", $matches);
But it matches both cases and return the full sentence.
Any help appreciated.
notice the
+after[a-zA-Z0-9]it says one or more alpha numeric characterthe
(and)around the whole expression would permit you to catch the whole expression.Since your content is around
[]I have changed.*to[^\]]since the regular expression are greedy in case oftest2[.....] test3[sadsdasdasdad]it would capture until the end since there is a].Also please note since you are using the
$it will match always things in the end, I am not really sure if it’s what you intend to do.You can see this for reference.