PHP Regular expression

In some applications, it is sometimes necessary to match(search) certain strings according to certain rules, such as requiring the user to enter a number that is at least 5 digits. The tool used to describe these rules is the regular expression.

The simplest match

The simplest match is the direct given character match. If a match with aabab characters, it will match the three results, respectively, the first string of characters 1, 2 and 4. This match is the simplest case, but often the actual processing will be much more complex, such as the following "QQ number of digits and at least five", the corresponding regular expression:


The regular expression to describe the need to determine the content of at least five or more digits. Let's take a look at how the expression describes the rule:

  1. ^: matches the beginning of the string, that is, the string is a separate start rather than included within a string
  2. d: Matches numbers
  3. {5,}: matches at least 5 digits
  4. $: indicates the end of the matching string, that is, the string is an independent end

Now it is clear that the regular expression is combined to match more than five consecutive digits, and there is a separate start and end, for less than five digits, or not a digital start and end such as a123456b are Invalid.

As you can see from this example, regular expressions are described from left to right.

Similarly, if the mobile number to match the regular expression is:



Because of the regular expression matching results, in many cases are not so sure, it is best to download a number of auxiliary tools for testing the regular expression matching results. Such tools as Match Tracer, RegExBuilder, etc., and other similar tools can be.


In the above example, ^, d and $these symbols, representing the specific meaning of the match, we call the metacharacter, commonly used metacharacters are as follows:

metacharacters Description
. matches any character except the newline character
w Match letters or numbers or underscores
s matches any whitespace
d Matches numbers
b matches the start or end of the word
^ Match the start of the string
$ Matches the end of the string
[x] Matches x characters such as,, andcharacters inmatching string
W w Antisense, which matches any non-letter, number, underscore, and kanji characters
S s antisense, that match any non-blank character
D d antisense, that match any non-numeric characters
B The ant b is not the beginning or end of the word
[^x] Matches any character except x that unexpectedly matches, such as[^ abc] matches any character except the letters abc


  1. When we want to match these metacharacters, we need to use the character escape function, the same regular expression inside with to escape, such as to match the symbol, you need to use \. Interpreted as "any character except the newline". Of course, to match \, you need to write \\
  2. Concatenation of numbers or letters can be concatenated with a - sign, such as a-z matching all lowercase letters,[1-5] matching 1 to 5 5 digits

Copyright 2014-2017 by All Rights Reserved.