Detailed explanation of Python regular expressions, tell you what Python regular expressions are?

Tomorin
Release: 2018-08-15 13:52:52
Original
2091 people have browsed it

Python regular expression is a special character sequence that can help you easily check whether a string matches a certain pattern. This article will explain Python regular expression in detailTell youRegular expressions are used a lot in python because they can perform any matching and match the information we want to extract. When we come into contact with Python regular, you will know the power of regularity. There is a regular library re. In some projects, we often call the regular library to solve matching-related problems.

Strings are the most commonly used data structure in programming, and the need to operate on strings is almost everywhere. For example, to determine whether a string is a legal email address, although you can programmatically extract the substrings before and after @, and then determine whether it is a word and a domain name, this is not only troublesome, but also difficult to reuse the code.

Regular expression is a powerful weapon used to match strings. Its design idea is to use a descriptive language to define a rule for a string. Any string that conforms to the rule is considered to "match". Otherwise, the string is illegal.

So the way we judge whether a string is a legal Email is:

1. Create a regular expression that matches Email;

2. Use this regular expression Formula to match the user's input to determine whether it is legal.

Because regular expressions are also represented by strings, we must first understand how to use characters to describe characters.

In regular expressions, if characters are given directly, it is an exact match. Use \d to match a number, \w to match a letter or number, so:

'00\d' can match '007', but cannot match '00A';

'\d\d\d' can match '010';

'\w\w\d' can match 'py3' ;

.
Copy after login

can match any character, so:
'py.' can match 'pya', 'pyb', 'py!' and so on.

To match variable-length characters, in regular expressions, use * to represent any number of characters (including 0), use to represent at least one character, use ? to represent 0 or 1 characters, and use {n } represents n characters, and {n,m} represents n-m characters:

Let’s look at a complex example: \d{3}\s \d{3,8}.

Let’s interpret it from left to right:

1. \d{3} means matching 3 numbers, such as '010';

2. \s can match a space (including tab and other whitespace characters), so \s means there is at least one space, such as matching ' ', ' ', etc.;

3. \d{3,8} represents 3-8 numbers, such as '1234567'.

Taken together, the above regular expression can match phone numbers with area codes separated by any number of spaces.

Detailed explanation of Python regular expressions, tell you what Python regular expressions are?

The above is the detailed content of Detailed explanation of Python regular expressions, tell you what Python regular expressions are?. For more information, please follow other related articles on the PHP Chinese website!

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