Home > Backend Development > PHP Tutorial > 正则表达式w和d的疑惑

正则表达式w和d的疑惑

WBOY
Release: 2016-06-06 20:09:21
Original
1487 people have browsed it

<code>$string='April15,2003';
$pattern='/(\w+)(\d+),(\d+)/i';
$replacement='${1},${3}';
echo preg_replace($pattern,$replacement,$string);</code>
Copy after login
Copy after login

?>
结果为April1,2003
为什么 (w+)匹配了April 1,而没有匹配April 15
是不是因为:w本身会匹配数字,如果把5也匹配了,那么d就没有可以匹配的东西了

回复内容:

<code>$string='April15,2003';
$pattern='/(\w+)(\d+),(\d+)/i';
$replacement='${1},${3}';
echo preg_replace($pattern,$replacement,$string);</code>
Copy after login
Copy after login

?>
结果为April1,2003
为什么 (w+)匹配了April 1,而没有匹配April 15
是不是因为:w本身会匹配数字,如果把5也匹配了,那么d就没有可以匹配的东西了

w+能够完整匹配April15,但是你在w+后面又写了d+,为了能够匹配你定义的规则,April1给了w,5给了d。
$pattern='([a-z]+)(\d+),(\d{4})/i';
是符合要求的

$pattern='/(w+)(d{1,2{+),(d+)/i';这样子了是不是楼主想要的日期了

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template