1、匹配用户名
规则:
- 允许字符和数字(a-z,A-Z,0-9)
- 允许下划线
正则表达式:
/^[a-zA-Z0-9_]{3,16}$/
代码示例:
function validate_username( $username ) {
if(preg_match(’/^[a-zA-Z0-9_]{3,16}$/’, $_GET['username'])) {
return true;
}
return false;
}
2、匹配XHTML或XML标签
正则表达式:
{]*>(.*?)}
代码示例:
function get_tag( $tag, $xml ) {
$tag = preg_quote($tag);
preg_match_all(’{<'.$tag.'[^>]*>(.*?)'.$tag.'>.’}',
$xml,
$matches,
PREG_PATTERN_ORDER);return $matches[1];
}
3、匹配确定属性值的XHTML或XML标签(例如:class或tag)
正则表达式:
{]*attribute\\s*=\\s*(["'])value\\\\1[^>]*>(.*?)}
代码示例:
function get_tag( $attr, $value, $xml, $tag=null ) {
if( is_null($tag) )
$tag = ‘\\w+’;
else
$tag = preg_quote($tag);$attr = preg_quote($attr);
$value = preg_quote($value);$tag_regex = “/<(".$tag.")[^>]*$attr\\s*=\\s*”.
“(['\\"])$value\\\\2[^>]*>(.*?)<\\/\\\\1>/”preg_match_all($tag_regex,
$xml,
$matches,
PREG_PATTERN_ORDER);return $matches[3];
}
4、匹配和解析email地址
代码示例(比较复杂些):
function is_valid_email_address($email){
$qtext = ‘[^\\x0d\\x22\\x5c\\x80-\\xff]‘;
$dtext = ‘[^\\x0d\\x5b-\\x5d\\x80-\\xff]‘;
$atom = ‘[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'.
'\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+’;
$quoted_pair = ‘\\x5c[\\x00-\\x7f]‘;
$domain_literal = “\\x5b($dtext|$quoted_pair)*\\x5d”;
$quoted_string = “\\x22($qtext|$quoted_pair)*\\x22″;
$domain_ref = $atom;
$sub_domain = “($domain_ref|$domain_literal)”;
$word = “($atom|$quoted_string)”;
$domain = “$sub_domain(\\x2e$sub_domain)*”;
$local_part = “$word(\\x2e$word)*”;
$addr_spec = “$local_part\\x40$domain”;return preg_match(”!^$addr_spec$!”, $email) ? 1 : 0;
}
5、匹配URL
正则表达式:
{
\\b
# Match the leading part (proto://hostname, or just hostname)
(
# http://, or https:// leading part
(https?)://[-\\w]+(\\.\\w[-\\w]*)+
|
# or, try to find a hostname with more specific sub-expression
(?i: [a-z0-9] (?:[-a-z0-9]*[a-z0-9])? \\. )+ # sub domains
# Now ending .com, etc. For these, require lowercase
(?-i: com\\b
| edu\\b
| biz\\b
| gov\\b
| in(?:t|fo)\\b # .int or .info
| mil\\b
| net\\b
| org\\b
| [a-z][a-z]\\.[a-z][a-z]\\b # two-letter country code
)
)# Allow an optional port number
( : \\d+ )?# The rest of the URL is optional, and begins with /
(
/
# The rest are heuristics for what seems to work well
[^.!,?;"\\'<>()\[\]\{\}\s\x7F-\\xFF]*
(
[.!,?]+ [^.!,?;"\\'<>()\\[\\]\{\\}\s\\x7F-\\xFF]+
)*
)?
}ix
完整的文章请访问这里阅读(英文)
Tags: 正则表达式 regular


0 Responses to “每个web程序员都应该知道的5个正则表达式”
Leave a Reply