Detailed Explanation of PHP Regular Expressions

by stilling2006 on 2008-11-16 16:07:00

This article is from "Incredible" http://bk41.com/blog.

Original: http://weblogtoolscollection.com/regex/regex.php

Translation: http://www.phpangel.cn/php/php-regex.html

Basic Syntax:

- `"^The"`: Starts with "The"

- `"of despair$"`: Ends with "of despair"

- `"^abc$"`: The exact string "abc"

- `"notice"`: Any string containing "notice"

As in the last example, if no boundary symbols are defined, it means searching for the string within the current string.

The most commonly used symbols `*`, `+`, and `?` respectively mean "appears zero or more times", "appears one or more times", and "appears zero or one time".

- `"ab*"`: Matches "a", "ab", "abbb"

- `"ab+"`: Matches "ab", "abbb", does not match "a"

- `"ab?"`: No "b" or only one "b"

- `"a?b+$"`: Ends with character "b" (where "a?" can be ignored)