首页 > 正则 匿名捕获 命名捕获

正则 匿名捕获 命名捕获

深知正则表达式的威力最近学习一些,偶遇问题,废话不多说上代码:

$str = 'Mr.Ck-@139.com';

if(preg_match('(?:<#user>[a-zA-Z0-9_-.]+)@(?:<#host>[a-zA-Z0-9_-.]+)', $str, $match)) {
    print_r($match);
}



$str = '123,456,78,90';

if(preg_match('(?[0-9]+)(.(?[0-9]+))*', $str, $match)) {
    print_r($match);
}

错误提示

Warning: preg_match(): Unknown modifier '@' in D:\webServ\index.php on line 15

Warning: preg_match(): Unknown modifier '(' in D:\webServ\index.php on line 23


我不知道是不是因为PHP函数的问题,我相信这个表达式应该是没问题的,只是preg_match这个函数的什么机制导致的把?


号码 最终问题 我解决了


'(?:<#user>[a-zA-Z0-9_-.]+)@(?:<#host>[a-zA-Z0-9_-.]+)'
(?[0-9]+)(.(?[0-9]+))*

改为

'/(?:<#user>[a-zA-Z0-9_-.]+)@(?:<#host>[a-zA-Z0-9_-.]+)/'
'/(?[0-9]+)(.(?[0-9]+))*/'

附改后的代码:

php<?php

$str = 'Mr.Ck-@139.com';

if(preg_match('/(?:<#user>[a-zA-Z0-9_\-.]+)@(?:<#host>[a-zA-Z0-9_\-.]+)/', $str, $match)) {
    print_r($match);
}



$str = '123,456,78,90';

if(preg_match('/([0-9]+)(.([0-9]+))*/', $str, $match)) {
    print_r($match);
}
【热门文章】
【热门文章】