分类目录 ‘RegEx’

str = ‘中华人民共和国123456789abcdefg’;
echo preg_match(“/^[\u4e00-\u9fa5_a-zA-Z0-9]{3,15}”,strName);

运行一下上面这段代码,看会有什么提示信息?
Warning: preg_match(): Compilation failed: PCRE does not support \L, \l, \N, \P, \p, \U, \u, or \X at offset 3 in F:\wwwroot\php\test.php on line 2

原来,PHP正则表达式中不支持下列 Perl 转义序列:\L, \l, \N, \P, \p, \U, \u, or \X

在 UTF-8 模式下,允许用“\x{…}”,花括号中的内容是表示十六进制数字的字符串。原来的十六进制转义序列 \xhh 如果其值大于 127 的话则匹配了一个双字节 UTF-8 字符。
所以,可以这样来解决preg_match(“/^[\x80-\xff_a-zA-Z0-9]{3,15}”,strName);

The regular expression, as a pattern, can match all kinds of text strings helping your application validate, compare, compute, decide etc. It can do simple or very complex string manipulations. The list of possibilities is enormous when it comes to what you can achieve using regular expressions. You can take any phrase that starts with an “A” or any character and do various things with it. You can match phone numbers, email addresses, url’s, credit card numbers, social security numbers, zip codes, states, cities…..(the list never ends). A huge script that is supposed to validate a user input and prepare it for sql can be reduced to only one line with the help of preg_replace.

mre

Mastering Regular Expressions quickly covers the basics of regular-expression syntax, then delves into the mechanics of expression-processing, common pitfalls, performance issues, and implementation-specific differences. Written in an engaging style and sprinkled with solutions to complex real-world problems, MRE offers a wealth information that you use. I will start with some simple usage examples of the regular expressions and continue with a huge list of cases for various situations where we would normally need a regex to operate. We will use simple functions which return TRUE or FALSE. $regex will serve as our regular expression to match against and $text will be our text (pretty obvious):
(全文…)

6个正则表达式工具

作者: seasun

正则表达式能够帮助用户和开发人员更加有效地查找和操纵文本内容。而且,正则表达式已经得到了许多脚本语言、编程语言和数据库的良好支持。就算你不是一个开发人员,而是一个垃圾站长,掌握正则表达式也能够让你事半功倍。

如果你不觉得正则表达式很难读写的话,要么你是一个天才,要么,你不是地球人。正则表达式的语法很令人头疼,即使对经常使用它的人来说也是如此。由于难于读写,容易出错,所以找一种工具对正则表达式进行测试是很有必要的。
(全文…)

<?php
$xml = “”;
$f = fopen( ‘books.xml’, ‘r’ );
while( $data = fread( $f, 4096 ) ) { $xml .= $data; }
fclose( $f );

preg_match_all( “/\<book\>(.*?)\<\/book\>/s”, $xml, $bookblocks );

foreach( $bookblocks[1] as $block ) {
preg_match_all( “/\<author\>(.*?)\<\/author\>/”, $block, $author );
preg_match_all( “/\<title\>(.*?)\<\/title\>/”, $block, $title );
preg_match_all( “/\<publisher\>(.*?)\<\/publisher\>/”, $block, $publisher );
echo( $title[1][0] . ” – ” . $author[1][0] . ” – ” . $publisher[1][0] . “\n” );
}
?>

正则表达式对于小量的XML数据解析比较高效,如果是大数据量的,那么就请使用 DOM 库读取 XML 或 SAX 解析器读取 XML 吧!!

正则表达式(Regular Expression, abbr. regex) 功能强大,能够用于在一大串字符里找到所需信息。它利用约定俗成的字符结构表达式来发生作用。不幸的是,简单的正则表达式对于一些高级运用,功能远远不够。若要进行筛选的结构比较复杂,你可能就需要用到高级正则表达式
(全文…)

Java正则表达式学习:
因为正则表达式是一个很庞杂的体系,此例仅举些入门的概念,更多的请参阅相关书籍及自行摸索。

\\ 反斜杠
\t 间隔 (‘\u0009′)
\n 换行 (‘\u000A’)
\r 回车 (‘\u000D’)
\d 数字 等价于[0-9]
\D 非数字 等价于[^0-9]
\s 空白符号 [\t\n\x0B\f\r]
\S 非空白符号 [^\t\n\x0B\f\r]
\w 单独字符 [a-zA-Z_0-9]
\W 非单独字符 [^a-zA-Z_0-9]
\f 换页符
(全文…)

常用的正则表达式

作者: seasun

/^\[ \t]*$/ “^\[ \t]*$” 匹配一个空白行。

/\d{2}-\d{5}/ “\d{2}-\d{5}” 验证一个ID号码是否由一个2位字,一个连字符以及一个5位数字组成。

/<(.*)>.*<\/\1>/ “<(.*)>.*<\/\1>” 匹配一个 HTML 标记。
下表是元字符及其在正则表达式上下文中的行为的一个完整列表:
(全文…)