Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Common uses of python regular expressions

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "the common uses of python regular expressions". In daily operation, I believe many people have doubts about the common uses of python regular expressions. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "the common uses of python regular expressions". Next, please follow the editor to study!

Common uses of regular expressions:

Extract substrings according to requirements in the pattern string

Some characters in the string or specified strings are replaced or deleted or separated.

Determine whether the input string is in the correct format when the user enters the user name

First, the foundation

Re modules match frequently used metacharacters and syntax

Character type:

Predefined character type:

Quantitative character type:

Boundary, logic, or group matching:

Greed mode and non-greed mode:

The quantifier of greed pattern is also called matching priority quantifier, which mainly includes: "{mheroin n}", "{m,}", "?", "*" and "+".

Add "?" after the matching priority quantifier. It is called ignoring priority quantifiers, which mainly includes: "{n}?", "{mrecoery n}?", "*?" And

"+?"

Greedy pattern: matches as many characters as possible when the matching character object is satisfied, referring to a string modified by matching priority quantifiers

Non-greedy pattern: match as few characters as possible when the matching rules are met, referring to strings that are matched by ignored priority quantifiers

Make a list to help you understand the difference between greedy and non-greedy:

Description: when the string traverses to the second, it already meets the matching rules, but the first pattern is greedy mode needs to match as many characters as possible, and the third is followed by no characters that can meet the matching rules. So after traversing to the third, the result is:. * 56; while pattern 2 is non-greedy mode, the result is 56.

The problem of backslash in matching

When using regular matching, some special characters need to be escaped, such as "baidu\ .com" for baidu.com', matching, but the python character itself is also escaped with'\', so it needs to be written as' baidu\ .com'. To avoid programming problems when writing matching character objects, we can add a letter r in front of the string and convert it to the original string for matching, such as r "baidu\ .com"

2. Functions commonly used in re templates

Re.compile ()

Re.compile () compiles a string into a schema object, making it easy to directly match and search or split by using functions such as search,findall,split.

Prototype of re.compile (): re.compile (pattern [, flags])

Pattern represents a string matching pattern object, and flags represents a modifier quantifier: for example: "*", "+"

Example of the compile function: re.compile (r "\ d +"): indicates that consecutive numbers are matched

Compile also has a parameter flag that allows you to set regular expression modifiers, which are commonly used as follows:

For the interpretation of re.X, for example, the two regular expressions in the figure are equivalent:

Re.match ()

The match function starts matching from the head of the string, and returns a correct object (object) when the character satisfies the matching pattern object. If it is not satisfied, the None; example is as follows:

The character header matches successfully:

String header matching failed

Match also has several commands that can continuously match multiple groups (represented by group (n) or groups ()), and the matching between groups is represented by tuples:

Match also has some ways to find locations and ranges that match the beginning and end of a set of strings, using the following methods:

Re.search ()

Search can be matched in any part of the string. Similar to match, if the pattern string satisfies the specified matching object for the first time, an object is returned. If neither is satisfied, the matching starting position of the pattern string can be specified. Of course, match, research, findall (functions to be described later) can be matched directly without the compile method. Examples are as follows:

Re.findall ()

Findall also matches any part of the string, but it is mainly different from search: when matching groups, search only returns the first group that satisfies the matching object, while findall returns all the groups that satisfy the matching object. Moreover, findall does not have the application of group or groups methods, so the matching value can be returned directly. Write an example and feel it:

Re.sub ()

Prototype of sub: sub (pat,repl,string [, count=0]); replace the character repl with pat in string

The purpose of the sub method is to replace a character or string in the pattern string with a character or string. Sub can also borrow matching objects in compile or match them directly, using the following methods:

Re.split ()

Prototype of split method: split (pattern,string [, maxsplit = 0])

Split function: split a string into a list based on matching character objects. Examples are as follows:

At this point, the study on "the common uses of python regular expressions" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report