Home > Backend Development > PHP Tutorial > Regular expressions for PHP development (1)

Regular expressions for PHP development (1)

WBOY
Release: 2016-08-08 09:27:43
Original
1076 people have browsed it

Speaking of regular expressions, I encountered them when I was learning compilation principles in college. I didn’t pay much attention to them at that time. I just understood a little bit. Later when I was making an app, I learned that this is just a point that is actually very important, no matter it is It is used in many places on websites and apps. In the past, every time I used it, I looked for ready-made ones on the Internet, but it was very painful to not be able to write regular expressions that met the requirements myself. Start learning below.

1. Row locators (^ and $)

Row locators are convenient for describing strings. "^" represents the beginning of the line, "$" represents the end of the line

For example: ^Jack represents a string starting with Jack, then "Jack is a hero" can match, but "The super man is Jack" cannot match

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

2. Word delimiter (b, B)

If you want to match a complete word instead of a word part, then you need to use word delimiters

For example: bworkb means that the word work is in the string Then "I'm work hard!" can match

For example: BworkB means that the word work cannot be included in the string Then "I'm work hard!" cannot match

3. Character class ([])

Regular expressions are case-sensitive. If you want to ignore case, you can use the square bracket expression "[]". As long as the matching word appears within square brackets, the match is successful. But please note that a square bracket can only match one character. For example, if you want to match a string ab that is not case-sensitive, the expression should be as follows: [Aa][Bb] This way you can match all the ways of writing ab

4. Select the character (|)

There is another way to achieve matching A string ab is not case-sensitive, that is, the selection character (|) is used. This character can be understood as "or", then the expression is: (A|a)(B|b)

5. Hyphen (-)

The naming rule of variables can only start with a letter and an underscore. But in this way, if you want to use a regular expression to match the first letter of a variable name, you have to write it as [a,b,c,d...A,B,C,D...] This is undoubtedly very troublesome Yes, the regular expression provides a hyphen "-" to solve this problem. A hyphen can represent a range of characters. For example, the above example can be written as [a-zA-Z]

6. Exclusion character ([^])

'^' represents the beginning of the line, and placing this character in square brackets means exclusion. For example: [^a-zA-Z] This expression matches variable names that do not start with letters or underscores.

7. Qualifier (? * + {n,m})

For repeated letters or strings, you can use qualifiers to achieve matching. There are 6 main types of qualifiers, as shown in the following table:


8. Dot character (.)

The dot character (.) can match any character except the newline character. Note: Any character other than a newline character

If it matches a word that starts with a and ends with b and contains a character in the middle. Then the format is as follows: ^a.b$

9. Escape characters ()

The escape characters in regular expressions are similar to the escape characters in php. They are special characters (such as: "." "?" " ") into ordinary characters. Take the IP address 127.0.0.1 as an example. If no escape characters are used in this format, the expression result of the regular expression is as follows: [1-9]{1,3}(.[1-9]{1,3}) {3} But this is obviously wrong, because "." can be matched with any character. This means not only IPs like 127.0.0.1 will appear, but also letters and other characters. This is obviously wrong. At this time, the escape character will be sent. Come in handy, please escape characters. The regular expression using escape characters is [1-9]{1,3}(.[1-9]{1,3}){3}

10. Backslash()

(1) Backslash can convert some unprintable characters It is displayed as shown in the following table:


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


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


11. Backreference

Backreference is to use the "memory" function of subexpressions to match consecutive strings or letters.

For example: If you want to match two consecutive ab, you can use ab as a group and then add "1" at the end. As follows: (ab)1

If the string to be matched is not fixed, you can write the string in brackets as a regular expression. If there are multiple groups, you can use "1", "2" to represent each group (the order is from left to Right)

For example: ([a-z])(A-Z)12

In addition to using numbers to represent groups, you can also specify the group name yourself, such as:

(?P...)

If To reference this group, the syntax is as follows:

(?P = subname)

Let’s rewrite the expression ([a-z])(A-Z)12. Name these two groups respectively and back-reference them. Regular expression The formula is as follows:

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

Let’s learn the theoretical knowledge together here, more More operations will be shared with you in Regular Expressions for PHP Development (2)

The above has introduced regular expressions in PHP development (1), including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template