


An introduction to regular expressions and their common matching functions in Python
/Introduction/
Python has added the re module since version 1.5, which Provides Perl-style regular expression patterns. The re module enables the Python language to have all regular expression functions.
The compile function generates a regular expression object based on a pattern string and optional flag parameters. This object has a series of methods for regular expression matching and replacement.
The re module also provides functions that are identical to these methods. These functions take a pattern string as their first argument.
##/re.match function/
re.match attempts to match a pattern from the starting position of the string. If the matching is not successful at the starting position, match() will return none. The syntax is as follows:
re.match(pattern, string, flags=0)
"pattern" matches the regular expression "string" and the string "flags" flag to match.
If the match is successful, the re.match method returns a matching object, otherwise it returns None.
We can use group(num) or groups() matching object function to get the matching expression.
group(num=0) 匹配的整个表达式的字符串,“group()”可以一次输入多个组号,在这种情况下它将返回一个包含那些组所对应值的元组。
下图是个实际例子:
输出结果如下图所示:
/检索和替换/
Python 的re模块提供了re.sub用于替换字符串中的匹配项。语法如下所示:
re.sub(pattern, repl, string, count=0, flags=0)
参数:
pattern : 正则中的模式字符串。
repl : 替换的字符串,也可为一个函数。
string : 要被查找替换的原始字符串。
count : 模式匹配后替换的最大次数,默认 0 表示替换所有的匹配。
flags : 编译时用的匹配模式,数字形式。
前三个为必参数,后两个为可选参数。
下图是个实际例子:
输出结果如下图所示:
/compile函数/
compile 函数用于编译正则表达式,供match() 和 search() 这两个函数使用。语法格式为:
re.compile(pattern[, flags])
参数:
pattern : 一个字符串形式的正则表达式
flags 可选,表示匹配模式,比如忽略大小写,多行模式等,具体参数为:
re.I 忽略大小写
re.L represents the special character set \w,\W, \b, \B, \s, \S depends on the current environment
re.M More Line pattern
re.S is '.' and any character including newline ('.' does not include newline)
re.U represents the special character set \w,\W, \b, \B, \d, \D, \s, \S and relies on the Unicode character attribute database
re.X To increase readability, ignore spaces and comments after '#'
/regular expression object/
re.RegexObject: re.compile() returns the 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 position of the match (start, end)
/regular expression modifier - optional flag/
Regular expressions can contain some optional flag modifiers to control the 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:
##re.I |
Make the match case-insensitive |
||||
re.L |
Do localization recognition (locale-aware) matching |
||||
re.M |
Multiple line matching, affecting ^ and $ |
||||
re.S |
# #Make . match all characters including newlines |
||||
re.U
|
Parses characters according to the Unicode character set. This flag affects \w, \W, \b, \B. |
||||
re. Regular expressions are written to be easier to understand. |
/regular expression pattern/ ## The pattern string uses special syntax to represent a regular expression Expression: Letters and numbers represent themselves. 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 if they are escaped, otherwise they represent a special meaning. The backslash itself needs to be escaped with a backslash. Since regular expressions usually contain backslashes, you'd better use raw strings to represent them. Pattern elements (such as r'\t', equivalent to \\t) match the corresponding special characters. The following table lists the special elements in the regular expression pattern syntax. If you use a pattern and provide optional flags arguments, the meaning of some pattern elements will change.
Character matching
Matches "python". 字符类 /实际应用/ 以猫眼电影为例。我们需要获取(电影的名字作者,上映时间)等等都可以用正则表达式来解析。 分析一下,利用正则表达式提取。 可以看到我们要的名字在一个a里面,而他们被一个div包裹着。 我们把div想象成一个盒子,可以看到div里面还有一个div 我们可以先找他上面一层的div是一个表单 分析完再实际操作一下: (.*?)表示我们要的内容 (.*?) /小结/ 1. Regular expressions are suitable for scenarios where multiple data needs to be obtained. It can get the data we want in a faster way. ## 2. This article mainly introduces regular expressions and their basic usage. For specific usage of each character, you can refer to the regular expression series articles in the preface. I hope it can help you learn more. Good understanding of the usage of regular expressions. |
The above is the detailed content of An introduction to regular expressions and their common matching functions in Python. For more information, please follow other related articles on the PHP Chinese website!

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function