Home > Backend Development > PHP Tutorial > 一个正则的问题

一个正则的问题

WBOY
Release: 2016-06-23 14:39:40
Original
798 people have browsed it

$string='dj%a3%a3999';if(preg_match('/^[dj]{2}([(\%23)(\%a3\%a3)])((\w)+)$/i', $string,$result)){ print_r($result);}
Copy after login

匹配不上,有什么好的解决方案。


回复讨论(解决方案)

你最好讲一下你的需求吧,中间那段看你的正则很乱。

/*需求就是dj一个分组%a3%a3 或者%23 是另外一个分组999是一个分组通过$result获取对应的数据。如:*/$result[0]='dj';$result[1]='%a3%a3';$result[2]='999';
Copy after login

$s = 'dj%a3%a3999';preg_match('/([^%]+)((?:%a3|23)+)(.+)/', $s, $r);print_r($r);
Copy after login
Copy after login
Array ( [0] => dj%a3%a3999 [1] => dj [2] => %a3%a3 [3] => 999 )

使用了这个:

$string='dj%a3%a3999';if(preg_match('/^([dj]{2})([%a3]+)(\d{3})$/i', $string,$result)){ print_r($result);}
Copy after login
Copy after login


结果:
Array ( [0] => dj%a3%a3999 [1] => dj [2] => %a3%a3 [3] => 999 )
Copy after login

$s = 'dj%a3%a3999';preg_match('/([^%]+)((?:%a3|23)+)(.+)/', $s, $r);print_r($r);
Copy after login
Copy after login
Array ( [0] => dj%a3%a3999 [1] => dj [2] => %a3%a3 [3] => 999 )

如果$s = 'dj%23999';则不行。

使用了这个:

$string='dj%a3%a3999';if(preg_match('/^([dj]{2})([%a3]+)(\d{3})$/i', $string,$result)){ print_r($result);}
Copy after login
Copy after login

[/code]
这个串也需要匹配的:$string='dj%23999';

$s = 'dj%23999';
preg_match('/([^%]+)((?:%(?:a3|23))+)(.+)/', $s, $r);

preg_match('/([^%]+)((?:%a3|%23)+)(.+)/', $s, $r);

正则表达式规则说明中有:
| 运算时如果发生歧义,则需要逐个开列或用括号括起来

显然 (?:%a3|23) 中 % 被结合到了 a3 从而发生了歧义

老大,能不能帮我解释下,我看到有很多的正则前面都加?:我想知道这个有什么意义?

(?:...) 与 (...) 分组作用一样,但结果不出现结果集中

(?:...) 与 (...) 分组作用一样,但结果不出现结果集中

老大,能不能帮我匹配个地址,如:

www.sohu.com,www.baidu.com,www.yahoo.com.n

这样连着的网址最多为9个,其中也可以匹配sohu.com,baidu.com,yahoo.com.cn

(?!(w))(\w+(-\w+)*)(\.(\w+(-\w+)*))*

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