A first look at Python regular expressions (6)

Continue to share the basics of Python regular expressions. The special symbol for regular expressions shared with you today is "[]". The square brackets are very practical and have special meanings, which means that the characters in the square brackets only need to satisfy any one of them. There are three kinds of usages, which will be demonstrated in specific code respectively, and summarized at the end. The specific tutorial is as follows.

1、 As shown in the figure below, the matching mode is [abcd], where the regular expression means that the first character of the string is any one of the four characters abcd, and the following character is "cpeng123", if the matching conditions are met , The result will be output, if it is not met, no result will be displayed, as shown in the figure below.

Obviously, the first character of the original string is d, which is connected to the matching pattern, so the result is output.

2、 In order to better understand, now the original string is changed to "acpeng123", and the other parts are unchanged, as shown in the figure below.

You can see that the matching result is "acpeng123" and the matching is successful.

3、 To further understand the meaning of the square brackets, now the original string is changed to "ecpeng123", and the other parts are unchanged, as shown in the figure below.

At this point, you can see that there is no result output, because there is no corresponding matching character in the brackets, which cannot meet the matching requirements, so there is no output.

4、 Look at the following example to extract the phone number, which is very common in practical applications. Here is another way of expressing square brackets, namely [0-9]. This special character represents any character from 0 to 9. The following matching pattern'(1[34578][0-9]{9})' means that the string starts with 1, and the second character is any one of 3, 4, 5, 7, and 8, after that The characters are numbers from 0 to 9, but are limited to 9 times, which means that the length of the phone number is 1+1+9=11 digits. If the above matching requirements are met, the output is successful, otherwise no characters are output.

As shown in the figure above, it is obvious that the original string meets the matching requirements, so there is an output result.

Expand knowledge: [az] represents 26 English lowercase letters; [AZ] represents 26 English uppercase letters.

5、 In order to further strengthen the understanding, change the original string to a number starting with 160, and then output, as shown in the figure below.

You can see that there is no output result.

6、 The third chapter of the usage of square brackets is [^], and the special character "^" is added in the square brackets, which means not and negated. For example, "[^1]" means that the character is not equal to 1. The following figure is a code demonstration.

You can see that the original string matches the matching pattern'(1[34578][^1]{9})' successfully, because after the second character, there is no 1 in the string, which matches the matching rule.

Even if non-digit characters appear in the original string, as long as they are not 1, they can be matched successfully, as shown in the following figure.

7、 In order to strengthen understanding, first change the number in the original string to '18042682515', add a 1 to the end of the string, and then the matching mode does not change, as shown in the figure below.

You can see that there is no output result at this time, because 1 appears in the original string, and the matching mode requires that 1 cannot appear, so the matching is unsuccessful.

8、 Finally, summarize the purpose of brackets in special characters.

  1. Any one character in the brackets, such as [abcd], represents any one of the four characters a, b, c, and d.

  2. Represents the interval, such as [0-9], which represents any one of the numbers 0 to 9. In the same way, [az], [AZ], the meaning of which is mentioned above, will not be repeated here.

  3. It means negation or negation. The special expression is [^]. For example, the matching pattern [^1] means that the matched character is not 1.

  4. The "." in square brackets, such as the matching pattern [.] or "", such as the matching pattern [], is purely representing the "." and "*" signs, and is no longer representative of special characters. The meaning of any character or multiple occurrences of, this point requires special attention.

Brackets are very commonly used in the process of pattern matching. I hope that all friends can understand and master it.

- - - End ---

Recommended Posts

A first look at Python regular expressions (6)
A quick introduction to Python regular expressions
First look at the new features of Ubuntu 17.10
python development [first article]
python_ regular expression learning