初学PHP正则表达式
1. 想要匹配开头5个字符为字母数字的字符串 check that the first five characters in the string are alpha numeric characters.
preg_match('/^[A-Za-z0-9]{5}/', $test_string);
preg_match('/^\w{5}/', $test_string);
\w specifies any alpha numeric characters plus the underscore character (_).
$test_string = "3w5_32fgdsg*$@#";
preg_match('/^\w{5}/', $test_string, $matches);
var_dump($matches);
output:
array(1) {
[0]=>
string(5) "3w5_3"
}
2. 工作中遇到需要解析HTTP Header信息,取出sid信息,代码如下
$s = "HTTP/1.1 100 Continue HTTP/1.1 302 FOUND Server: nginx/1.1.12 Date: Wed, 17 Oct 2012 11:40:40 GMT Content-Type: text/html; charset=utf-8 Transfer-Encoding: chunked Connection: keep-alive Vary: Cookie Location: http://192.168.100.99:8360/login.php?s;
观察头信息可知,s/s +表示匹配一到多次的字母和数字的组合,为了能取出sid的值需要 '/s 用()括起来,最后能从$regs数组中取到
preg_match( '/s, $s, $regs);
var_dump($regs);
输出如下:
array(2) {
[0]=>
string(36) "s
[1]=>
string(32) "d7852c9c63a3ef9f6c48cda1a0503e7f"
}
3. php匹配并取小括号中的字符串
$pattern = '/.*\((.*)\).*/';
$subject = 'sdfs(sss)';
print preg_match($pattern, $subject, $matches);
print_r($matches);
1Array
(
[0] => sdfs(sss)
[1] => sss
)
4. 用grep取出包含7到8位电话号码的行(首号码不为0)
grep "[1-9][0-9]\{6,7\}" data.txt
具体php-regular-expressions的语法参考http://www.noupe.com/php/php-regular-expressions.html
5.
<?php $domain_list = array( 'tmall.com', 'alibaba.com', 'taobao.com', 'jd.com', ); $host = 'tmall.com'; preg_match("/(".implode("|", $domain_list).")$/iU", $host, $matches); var_dump($matches);
i表示不区分大小写, U表示不要贪婪
array(2) {
[0]=>
string(9) "tmall.com"
[1]=>
string(9) "tmall.com"
}
- 上一篇 »PHP正则表达式基础语法
- 下一篇 »PHP常用正则表达式汇总