PHP 正则表达式特殊字符 [:alnum:] [:alpha:] 等

 更新时间:2011年09月13日 11:28:41   作者:  
正则表达式中有两个很重要的特殊字符就是"[ ]"。他们可以匹配"[]"之中出现过的字符,比如"/[az]/"可以匹配单个字符"a"或者"z";如果把上面的表达式改成这样"/[a-z]/",就可以匹配任何单个小写字母,比如"a"、"b"等等。
如果在"[]"中出现了"^",代表本表达式不匹配"[]"内出现的字符,比如"/[^a-z]/"不匹配任何小写字母!并且正则表达式给出了几种"[]"的默认值,如下:

'[:alnum:]' 匹配任何字母
Alphanumeric characters: '[:alpha:]' and '[:digit:]'.

'[:alpha:]' 匹配任何字母和数字
Alphabetic characters: '[:lower:]' and '[:upper:]'.

'[:blank:]'
Blank characters: space and tab.

'[:cntrl:]'
Control characters. In ASCII, these characters have octal codes 000 through 037, and 177 ('DEL'). In other character sets, these are the equivalent characters, if any.

'[:digit:]' 匹配任何数字
Digits: '0 1 2 3 4 5 6 7 8 9'.

'[:graph:]'
Graphical characters: '[:alnum:]' and '[:punct:]'.

'[:lower:]' 匹配任何小写字母
Lower-case letters: 'a b c d e f g h i j k l m n o p q r s t u v w
x y z'.
'[:print:]'
Printable characters: '[:alnum:]', '[:punct:]', and space.

'[:punct:]' 匹配任何标点符号
Punctuation characters: '! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ' { | } ~'.

'[:space:]' 匹配空格符
Space characters: tab, newline, vertical tab, form feed, carriage
return, and space.

'[:upper:]' 匹配任何大写字母
Upper-case letters: 'A B C D E F G H I J K L M N O P Q R S T U V W
X Y Z'.

'[:xdigit:]' 匹配任何16进制数字
Hexadecimal digits: '0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f'.

For example, '[[:alnum:]]' means '[0-9A-Za-z]', except the latter depends upon the 'C' locale and the ASCII character encoding, whereas the former is independent of locale and character set. (Note that the brackets in these class names are part of the symbolic names, and must
be included in addition to the brackets delimiting the bracket expression.)

[$]方括号内包含的是一个匹配字符范围,前面加上^,即是代表不匹配指定的字符范围。
$ereg代表的是一个变量,即$是变量的标志,这个式子的整体意思就是:
ereg代表的是一个具有以字母开头第二个是字母或数字的所有字符串。

例子:
复制代码 代码如下:
preg_replace("/([[:alnum:]]|[[:space:]]|[[:punct:]])+/U", '', $string);

相关文章

  • SQL正则表达式错误 “parentheses not balanced“ 问题的排查和解决方案

    SQL正则表达式错误 “parentheses not balanced“ 问题

    在实际开发中,数据库查询经常需要使用正则表达式(REGEXP)来筛选复杂条件下的数据,然而,一些开发者在操作中可能会遇到类似以下错误parentheses not balanced,所以本文给大家介绍了SQL正则表达式错误 “parentheses not balanced“ 问题的排查和解决方案
    2024-12-12
  • Scala的文件读写操作与正则表达式

    Scala的文件读写操作与正则表达式

    这篇文章主要介绍了Scala的文件读写操作与正则表达式的相关知识,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
    2018-09-09
  • js:校验IPv6地址的正则表达式

    js:校验IPv6地址的正则表达式

    js:校验IPv6地址的正则表达式...
    2007-04-04
  • python 正则表达式 反斜杠(/)的麻烦和陷阱

    python 正则表达式 反斜杠(/)的麻烦和陷阱

    这里是一点小心得。 由于正则表达式使用反斜杠来转义特殊字符,而python自身处理字符串时,反斜杠也是用于转义字符,这样就产生了一个双重转换的问题
    2009-08-08
  • asp.net(c#) 使用Rex正则来生成字符串数组的代码

    asp.net(c#) 使用Rex正则来生成字符串数组的代码

    我们在项目中,经常要使用正则表达式,现在有一个工具可以按正则表达式生成数据.正则表达式的语法忘记了吗?
    2010-05-05
  • 读懂正则表达式就这么简单

    读懂正则表达式就这么简单

     对于正则表达式,相信很多人都知道,但是很多人的第一感觉就是难学,因为看第一眼时,觉得完全没有规律可寻,其实也没有你想象的那么难,今天小编就通过本文带领大家一起去学习正则表达式知识
    2016-11-11
  • js正则表达式讲解之index属性(RegExp对象)

    js正则表达式讲解之index属性(RegExp对象)

    index与search返回的值相同,不管是是否匹配到合适内容,如果找到合适内容则index为匹配内容其实字符的起始位置,如果没有找到则返回-1
    2012-10-10
  • 一看就懂的正则表达式教程

    一看就懂的正则表达式教程

    正则表达式,是简单地字符的序列,可指定特定的搜索模式,下面这篇文章主要给大家介绍了一看就懂正则表达式的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-05-05
  • PHP中基于perl的正则表达式处理函数

    PHP中基于perl的正则表达式处理函数

    和正则表达式一样,正则表达式处理函数不能够独立使用,而这必须相结合,才能够完成特定的功能。我们只介绍以preg开头的基于perl的正则表达式。
    2015-10-10
  • 将 html 转成 ubb代码的小脚本

    将 html 转成 ubb代码的小脚本

    将 html 转成 ubb代码的小脚本...
    2006-11-11

最新评论