Home > Backend Development > PHP Tutorial > 求助个正则匹配

求助个正则匹配

WBOY
Release: 2016-06-23 14:10:22
Original
995 people have browsed it

怎么匹配link/3445/65这样的形式,数字都是随机的,最后的数字可能没有
link/?([0-9]{1,})/?([0-9]{1,})/?$这样写怎么不对啊,不太懂,求助高手


回复讨论(解决方案)

<?php$subject = "link/3445/65";$pattern = '#link/?([0-9]*)/?([0-9]*)/?$#';preg_match($pattern, $subject, $matches);print_r($matches);?>
Copy after login

$s = "link/3445/644445";$reg = '/link\/([\d]*)\/([\d]*)$/i';preg_match($reg, $s, $a);print_r($a);
Copy after login
Copy after login
Copy after login

首先,你要是不需要获取匹配里面的具体内容,不要到处加括号,括号是用来获取里面内容的。

$reg = '/link\/\d+\/\d*$/i';

$s = "link/3445/644445";$reg = '/link\/([\d]*)\/([\d]*)$/i';preg_match($reg, $s, $a);print_r($a);
Copy after login
Copy after login
Copy after login

我觉得[\d]*有点多余,中括号代表或者的意思,自己一个元素没有或者,可以直接放在外面。个人理解~


$s = "link/3445/644445";$reg = '/link\/([\d]*)\/([\d]*)$/i';preg_match($reg, $s, $a);print_r($a);
Copy after login
Copy after login
Copy after login

我觉得[\d]*有点多余,中括号代表或者的意思,自己一个元素没有或者,可以直接放在外面。个人理解~
多谢指导,其实我对正则还不太熟。

学到了,多谢诸位了

Related labels:
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