深知正则表达式的威力最近学习一些,偶遇问题,废话不多说上代码:
<code>$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);
}
</code>错误提示:
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这个函数的什么机制导致的把?
深知正则表达式的威力最近学习一些,偶遇问题,废话不多说上代码:
<code>$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);
}
</code>错误提示:
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这个函数的什么机制导致的把?
preg_match 的第一个参数为正则的字符串, 正则需要开始和结束标记你这里没有.<code>'(?:<#user>[a-zA-Z0-9_-.]+)@(?:<#host>[a-zA-Z0-9_-.]+)' (?[0-9]+)(.(?[0-9]+))* </code>
改为
<code>'/(?:<#user>[a-zA-Z0-9_-.]+)@(?:<#host>[a-zA-Z0-9_-.]+)/' '/(?[0-9]+)(.(?[0-9]+))*/' </code>
_-. 中括号内的 - 线是有特殊含义的(请参考a-z, 0-9).(? 这样的写法是不正确的, PHP 的报错信息为:Warning: preg_match(): Compilation failed: unrecognized character after (? or (?- at offset 2 in T:\1.php on line xx 因为不知道你第二个具体要做啥, 可以直接删除 ? 号, 或者在 ? 号后面加上 : 号解决报错的问题.附改后的代码:
<code>php</code><code><?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);
}
</code>
号码 最终问题 我解决了
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号