In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the regular expressions and their common matching functions in Python. It is very detailed and has a certain reference value. Friends who are interested must read it!
A regular expression is a special sequence of characters that helps you easily check whether a string matches a pattern.
/ introduction /
Python has added the re module since version 1.5, which provides Perl-style regular expression patterns. The re module makes the Python language have all the regular expression functions.
The compile function generates a regular expression object based on a pattern string and optional flag parameters. The object has a series of methods for regular expression matching and replacement.
The re module also provides functions that are exactly the same as those of these methods, which take a pattern string as their first argument.
/ re.match function /
Re.match attempts to match a pattern from the beginning of the string, and match () returns none if the match is not successful. The syntax is as follows:
Re.match (pattern, string, flags=0)
The string "flags" flag bit of the regular expression "string" to match with "pattern".
If the match succeeds, the re.match method returns a matching object, otherwise it returns None.
We can use the group (num) or groups () match object function to get the matching expression.
The string of the entire expression matched by group (num=0), and "group ()" can enter more than one group number at a time, in which case it returns a tuple containing the values corresponding to those groups.
The following figure is a practical example:
The output is shown in the following figure:
/ retrieve and replace /
Python's re module provides re.sub to replace matches in strings. The syntax is as follows:
Re.sub (pattern, repl, string, count=0, flags=0)
Parameters:
Pattern: the pattern string in the regular.
Repl: the replacement string, which can also be a function.
String: the original string to be found and replaced.
Count: the maximum number of substitutions after a pattern match. The default of 0 means replacing all matches.
Flags: the matching pattern used at compilation time, in digital form.
The first three are required parameters and the last two are optional parameters.
The following figure is a practical example:
The output is shown in the following figure:
/ compile function /
The compile function is used to compile regular expressions for use by the match () and search () functions. The syntax format is:
Re.compile (pattern [, flags])
Parameters:
Pattern: a regular expression in string form
Flags is optional, indicating matching patterns, such as ignoring case, multiline mode, etc. The specific parameters are:
Re.I ignores case
Re.L indicates that the special character set\ w,\ W,\ b,\ B,\ s,\ S depends on the current environment
Re.M multiline mode
Re.S means'. 'and any character including a newline character ('. 'excluding newline characters)
Re.U represents the special character set\ w,\ W,\ b,\ B,\ d,\ D,\ s,\ S depending on the Unicode character attribute database
Re.X ignores spaces and comments after'#'to increase readability
/ regular expression object /
Re.RegexObject:re.compile () returns a RegexObject object.
Re.MatchObject:group () returns the string matched by RE.
Start () returns the position where the match starts
End () returns the position where the match ends
Span () returns a tuple containing the location of the match (start, end)
/ regular expression modifier-optional flag /
Regular expressions can contain optional flag modifiers to control matching patterns. The modifier is specified as an optional flag. Multiple flags can be specified by bitwise OR (|) them. For example, re.I | re.M is set to I and M flags:
/ regular expression pattern /
Pattern strings use special syntax to represent a regular expression:
Letters and numbers represent themselves. The letters and numbers in a regular expression pattern match the same string.
Most letters and numbers have different meanings when preceded by a backslash.
Punctuation marks match themselves only when they are escaped, otherwise they represent special meanings.
The backslash itself needs to be escaped using the backslash.
Since regular expressions usually contain backslashes, you'd better use raw strings to represent them. Pattern elements (such as r'\ tbrush, equivalent to\ t) match the corresponding special characters.
The following table lists the special elements in the regular expression pattern syntax. If you provide optional flag parameters while using the pattern, the meaning of some pattern elements will change.
Regular expression instance
Character matching
Character class
/ practical application /
Take the cat's eye movie as an example. What we need to get (the name of the movie, the author, the release time), etc., can be parsed with regular expressions.
Analyze it and extract it using regular expressions.
You can see that the names we want are in an a, and they are wrapped in a div.
We think of div as a box, and we can see that there is a div in the div. We can first find the div above it, and then find the box above it. Div, generally speaking, we can find the results we want when we find the first two layers. If it's not right, find a few more floors.
After the analysis, let's do it in practice:
Pattern = re.compile ('. *? title= "(. *?). *? class=" star "> (. *?)
. *? releasetime "> (. *?)
', re.S)
(. *) Express what we want (. *?)
The leading actor is also what we want so that we can get more data we want.
/ Summary /
1. Regular expressions are suitable for scenarios that need to get multiple data. It can get the data we want in a faster way.
two。 This Python tutorial mainly introduces regular expressions, and their basic usage, specific usage of each character, you can refer to the regular expression series in the preface, hoping to help you better understand the usage of regular expressions.
That's all of the article "what are regular expressions and their common matching functions in Python?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.