开发者

PHP preg_match_all regex expression weirdness

I am having some trouble with regex in php (preg_match_all).

I am using the following code to find an email encapsulated by <>:, i.e. :

preg_match_all("<[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})>:", $body,$matches);

For some reason PHP is blowing up at the colon with the following error:

Warning: preg_match_all() [function.preg-match-all]: Unknown modifier ':' in...

Any help would be 开发者_运维技巧much appreciated, as I am no regex guru, and am just about out of hair to pull.


You need to use delimeters EX:

preg_match_all('/<[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})>:/', $body,$matches);

See the / I added on both ends telling PHP where the regex starts and ends.


You could use T-Regx which doesn't need / at the start and end

$pattern = "<[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})>:";

$matches = Pattern::of($pattern)->match($body)->all();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜