开发者

Php preg_match help

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 match开发者_StackOverflow中文版es both cases and return the full sentence.

Any help appreciated.


preg_match('/([a-zA-Z0-9]+[\[][^\]]+[\]])$/i',"test1 test2[...]", $matches);

notice the + after [a-zA-Z0-9] it says one or more alpha numeric character

the ( 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 of test2[.....] 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜