Regular expressions for php development (1)

Source: Internet
Author: User
Tags character classes printable characters
Regular expressions for php development (1) & nbsp; when talking about regular expressions, I encountered the compilation principles in college, and did not pay much attention to them at that time, I only learned a little about it. later I learned that this is only important when I was working on an App. it was used in many places, both websites and apps, I used to search for regular expressions on the internet every time, but I cannot write regular expressions that meet the requirements. this is a very painful task. The following is a regular expression for php development (1)

Speaking of regular expressions, I encountered it when I learned how to compile in college, and I didn't pay much attention to it at that time. I just learned a little about it, later, when I was doing an App, I learned that this is just a very important point. it is used in both websites and many apps. I used to search for the ready-made products on the internet each time, however, it is very painful to write regular expressions that meet the requirements on your own. Next, let's start learning.

1. row Locator (^ and $)

The line locator is used to describe strings conveniently. "^" Indicates the beginning of the row, and "$" indicates the end of the row.

For example, if ^ Jack indicates a string starting with Jack, "Jack is a hero" can be matched, while "The super man is Jack" cannot.

For example, if Jack $ represents a string ending with Jack, then "The super man is Jack" can match


2. word delimiters (\ B, \ B)

If you want to match a complete word instead of a part of a word, you need to use the word delimiter.

For example, \ bwork \ B indicates that the word "I'm work hard!" is contained in the string! "To match

For example, \ Bwork \ B indicates that the word "I'm work hard!" cannot be contained in a string! "Cannot match


3. character classes ([])

Regular expressions are case-sensitive. to ignore the case, use the square brackets expression "[]". As long as the matching result appears in square brackets, the matching is successful. However, note that only one square brackets can match one character. For example, to match a string AB with case-insensitive characters, the expression should be as follows: [Aa] [Bb] to match all AB statements.

4. select the character (|)

Another method is to match a string AB case-insensitive, that is, to use the selected character (| ). This character can be understood as "or", so the expression is: (A | a) (B | B)


5. hyphens (-)

The naming rules for variables can only start with letters or underscores. However, if you want to use a regular expression to match the first letter of the variable name, write it as [a, B, c, d... a, B, C, D...] this is undoubtedly very troublesome. the regular expression provides the hyphen "-" to solve this problem. A hyphen can indicate the character range. The preceding example can be written as [a-zA-Z].


6. exclude characters ([^])

'^' Indicates the beginning of the line, and put this character in square brackets to indicate exclusion. For example, [^ a-zA-Z] the expression matches variable names that do not begin with letters or underscores.


7. the qualifier (? * + {N, m })

You can use a qualifier to match repeated letters or strings. There are six types of delimiters, as shown in the following table:



8. Periods (.)

The dot character (.) can match any character except the line break. Note: Any character except line breaks

For example, match a word that starts with a and ends with a character. The format is as follows: ^ a. B $


9. escape characters (\)

The escape characters in regular expressions are similar to those in php :".""? "" \ ") Is a common character. Take the IP address as an example. if the format 127.0.0.1 does not use escape characters, the expression result of the regular expression is as follows: [1-9] {1, 3 }(. [1-9] {1, 3}) {3} but this is obviously incorrect, because ". "It can be configured with any character. this will not only contain IP addresses like 127.0.0.1, but also letters and other characters. this is obviously not the case. it is useful to escape characters at this time, escape characters are available. The regular expression after escape characters is [1-9] {1, 3} (\. [1-9] {1, 3}) {3}


10. backslash (\)

(1) the backslash can display some non-printable characters as shown in the following table:


(2) the backslash can also specify the predefined character set as shown in the following table:


(3) the backslash can also define assertions as shown in the following table:



11. reverse reference

Reverse reference is to use the "memory" function of the subexpression to match consecutive strings or letters.

For example, to match two consecutive AB groups, you can use AB as a group and add "\ 1" to it. (AB) \ 1

If the string to be matched is not fixed, you can write the strings in parentheses as regular expressions. if there are multiple groups, you can use "\ 1 ", "\ 2" indicates each group (from left to right)

Example: ([a-z]) (A-Z) \ 1 \ 2

In addition to numbers, you can specify the group name by yourself, for example:

(? P ...)

To reference this group, the syntax is as follows:

(? P = subname)

Here we will rewrite the expression ([a-z]) (A-Z) \ 1 \ 2 for the two groups named respectively, and reverse reference them, the regular expression is as follows:

(? P [A-z]) (? P [A-Z]) (? P = fir )(? P = sec)


Let's take a look at the theoretical knowledge here. more operations will be shared with you in the regular expression (2) developed by PHP.

















Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.