Dickxyg 2012-03-13

代码如下:
$regex = '/^http:\/\/([\w.]+)\/([\w]+)\/([\w]+)\.html$/i';
$str = 'http://www.youku.com/show_page/id_ABCDEFG.html';
$matches = array();
if(preg_match($regex, $str, $matches)){
var_dump($matches);
}
echo "\n"; 代码如下:
$regex = '#^http://([\w.]+)/([\w]+)/([\w]+)\.html$#i';
$str = 'http://www.youku.com/show_page/id_ABCDEFG.html';
$matches = array();
if(preg_match($regex, $str, $matches)){
var_dump($matches);
}
echo "\n"; 代码如下:
$regex = '/HELLO/';
$str = 'hello word';
$matches = array();
if(preg_match($regex, $str, $matches)){
echo 'No i:Valid Successful!',"\n";
}
if(preg_match($regex.'i', $str, $matches)){
echo 'YES i:Valid Successful!',"\n";
} 代码如下:
$regex = '/(?<=c)d(?=e)/'; /* d 前面紧跟c, d 后面紧跟e*/
$str = 'abcdefgk';
$matches = array();
if(preg_match($regex, $str, $matches)){
var_dump($matches);
}
echo "\n"; 代码如下:
$regex = '/(?<!c)d(?!e)/'; /* d 前面不紧跟c, d 后面不紧跟e*/
$str = 'abcdefgk';
$matches = array();
if(preg_match($regex, $str, $matches)){
var_dump($matches);
}
echo "\n"; 代码如下:
$regex = '/HE(?=L)LO/i';
$str = 'HELLO';
$matches = array();
if(preg_match($regex, $str, $matches)){
var_dump($matches);
}
echo "\n"; 代码如下:
$regex = '/HE(?=L)LLO/i';
$str = 'HELLO';
$matches = array();
if(preg_match($regex, $str, $matches)){
var_dump($matches);
}
echo "\n"; 代码如下:
$regex = '/^(Chuanshanjia)[\w\s!]+\1$/';
$str = 'Chuanshanjia thank Chuanshanjia';
$matches = array();
if(preg_match($regex, $str, $matches)){
var_dump($matches);
}
echo "\n"; 代码如下:
$regex = '/(?P<author>chuanshanjia)[\s]Is[\s](?P=author)/i';
$str = 'author:chuanshanjia Is chuanshanjia';
$matches = array();
if(preg_match($regex, $str, $matches)){
var_dump($matches);
}
echo "\n"; 
代码如下:
$regex = '/(")[^\1]+\1/i';
$str = '"a""b""c""d"';
$matches = array();
if(preg_match($regex, $str, $matches)){
var_dump($matches);
}
echo "\n"; 
代码如下:
$regex = '/(")[^\1]+?\1/i';
$str = '"a""b""c""d"';
$matches = array();
if(preg_match($regex, $str, $matches)){
var_dump($matches);
}
echo "\n"; 
代码如下:
$regex = '/
^host=(?<!\.)([\d.]+)(?!\.) (?#主机地址)
\|
([\w!@#$%^&*()_+\-]+) (?#用户名)
\|
([\w!@#$%^&*()_+\-]+) (?#密码)
(?!\|)$/ix';
$str = 'host=192.168.10.221|root|123456';
$matches = array();
if(preg_match($regex, $str, $matches)){
var_dump($matches);
}
echo "\n"; | 特殊字符 | 解释 |
| * | 0到多次 |
| + | 1到多次还可以写成{1,} |
| ? | 0或1次 |
| . | 匹配除换行符外的所有单个的字符 |
| \w | [a-zA-Z0-9_] |
| \s | 空白字符(空格,换行符,回车符)[\t\n\r] |
| \d | [0-9] |