Home  >  Article  >  php教程  >  php5与mysql5 web 开发技术详解-5 正则表达式

php5与mysql5 web 开发技术详解-5 正则表达式

WBOY
WBOYOriginal
2016-06-13 10:55:22916browse

正则表达式(Regular Expression) 是查找和替换字符串模式的简洁和灵活的表示法。

重要性不再赘述,理解起来不算难,但若没有一定的实践,做到灵活应用还是不容易的。

在php中,分为 POSIX和PCRE两种规格的正则表达式。

先简单罗列出主要内容,以后再增加说明和实例。

 

 

一、POSIX (Portable Operating System Implementation for Unix),意为Unix可移植操作系统实现接口。

1、^ 和$ 定位符

2、量词或限定词

      *

      +

      ?

       {n}

       {n,}

       {n,m}

3、方括号表达式

       [aAeEiIoOuU]   所有元音字符的字符簇

       [0-9]   [a-z]

4、预定义字符簇  (内置的通用字符簇,指定字符的处理范围)

      [[:alpha:]]       大小写字母,同[a-zA-Z]

      [[:digit:]]          数字,同[0-9]

      [[:alnum:]]     大小写字母和数字,同[a-zA-Z0-9]

      [[:cntrl:]]         控制字符,包括Tab,退格或反斜线

      [[:space:]]      任何白字符,包括空格、Tab、换行、换页和回车

      ......

      不一一列举

5、正则表达式函数

     ereg(条件,)

     eregi()   不区分大小写

     ereg_replace()  :   ereg()的基础上增加了字符替换功能

     eregi_replace()

     split()  针对字符串中的分隔符对串分割成数组形式

     spliti()

     sql_regcase()

6 、POSIX 子模式

 

二、PCRE(Perl Compatible Regular Expression)

  1、字符簇

     \\b

     \\d

     \\s

     \\t

     \\w

  2、匹配

     表达式的开始和结束处 使用分隔符 / ,最后一个分隔符/后,可添加修饰符

     I ,M ,S,X,U,DU

     还可以使用各种元字符,类似 POSIX的定位符和字符簇的组合

     \A ,\b,\B,\d,\D,\s,\S,[],(),^,$,. ,\, \w ,\W

  3、函数www.2cto.com

     preg_match()  :  

           例:preg_match('/^[[:alnum]]{4,8}$/', $username)    

     preg_match_all()   

     preg_quote()  : 加转意字符

     preg_split() :

     preg_grep() 

     preg_replace() :替换

     preg_replace_callback()

 

Statement:
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