Summarize and organize python regular expression analysis
This article brings you relevant knowledge about python, which mainly introduces issues related to Python regular expressions, and summarizes regular expression functions, metacharacters, special sequences, and sets. Sets, matching objects, etc. I hope this helps.
Recommended learning: python tutorial
What is the role of regular expressions? There is a lot of content captured by our web page, and we cannot get all of it. We only need part of it, so we need to use regular expressions to match the content we want.
Regular Expression Module
Python has a built-in package called re that can be used to handle regular expressions. Import the re module:
import re
Regular Expressions in Python
After importing the re module, you can start using regular expressions.
For example: Search a string to see if it starts with "The" and ends with "Spain":
import re txt = "The rain in Spain"x = re.search("^The.*Spain$", txt)if x: print("匹配成功!")else: print("匹配失败")
Run:
Of course, you can't understand this right now For example, since we teach step by step, we will not teach you to reach the sky in one step.
Regular expression function
findall() function
The findall() function returns a list containing all matches.
Example: Print a list of all matches
import re txt = "川川菜鸟啊菜鸟啊"x = re.findall("菜鸟", txt)print(x)
Running returns:
The list contains matches in the order they are found. If no match is found, an empty list is returned:
import re txt = "菜鸟并不菜"x = re.findall("川川", txt)print(x)if (x): print("匹配成功了哟")else: print("找不到这个呀!")
Running returns:
##search() function
The search() function searches a string for a match and returns a Match object if there is a match. If there are multiple matches, only thefirst occurrence of the match is returned. For example: Search for the first whitespace character in the string:
import re txt = "菜鸟 呢"x = re.search("\s", txt)print("第一个空格字符位于位置:", x.start())
If no match is found, None returns this value:
import re txt = "天上飞的是菜鸟"x = re.search("川川", txt)print(x)
For example: Split at each whitespace character
import re txt = "菜鸟 学 python"x = re.split("\s", txt)print(x)
You can control the number of occurrences by specifying the maxsplit parameter
For example: Only at the Split the string on one occurrence:
import re#Split the string at the first white-space character:txt = "飞起来 菜鸟 们"x = re.split("\s", txt, 1)print(x)
sub() function
The sub() The function replaces matches with text of your choice. For example: Run with only replacement
import re txt = "学python就找川川菜鸟"x = re.sub("就", "只", txt)print(x)
You can control the number of replacements by specifying the count parameter:
For example, replace the first 2 occurrences:
import re txt = "学python就就就川川菜鸟"x = re.sub("就", "只", txt,2)print(x)
List symbols
[] Use On a set of characters For example: #Find all lowercase characters between "a" and "m" in alphabetical order
import re txt = "apple chuanchuan "#按字母顺序查找“a”和“m”之间的所有小写字符x = re.findall("[a-m]", txt)print(x)
Escape character
** represents a special sequence (can also be used to escape special characters) For example, match all numbers:
import re txt = "我今年20岁了"#查找所有数字字符x = re.findall("\d", txt)print(x)
Any symbol
. can be any character (except newline). For example: Search for a sequence starting with "he", followed by two (any) characters and an "o"
import re txt = "hello world"#搜索以“he”开头、后跟两个(任意)字符和一个“o”的序列x = re.findall("he..o", txt)print(x)
Start symbol
^ symbol is used to match the beginning.import re txt = "川川菜鸟 飞起来了"x = re.findall("^川", txt)if x: print("哇,我匹配到了")else: print("哎呀,匹配不了啊")
结束符
$ 符号用于匹配结尾,例如:匹配字符串是否以“world”结尾
import re txt = "hello world"#匹配字符串是否以“world”结尾x = re.findall("world$", txt)if x: print("匹配成功了耶")else: print("匹配不到哦")
运行:
星号符
- 星号符用于匹配零次或者多次出现。
import re txt = "天上飞的是菜鸟,学python找川川菜鸟!"#检查字符串是否包含“ai”后跟 0 个或多个“x”字符:x = re.findall("菜鸟*", txt)print(x)if x: print("匹配到了!")else: print("气死了,匹配不到啊")
运行:
加号符
+ 用于匹配一次或者多次出现
例如:检查字符串是否包含“菜鸟”后跟 1 个或多个“菜鸟”字符:
import re txt = "飞起来了,菜鸟们!"#检查字符串是否包含“菜鸟”后跟 1 个或多个“菜鸟”字符:x = re.findall("菜鸟+", txt)print(x)if x: print("匹配到了!")else: print("烦死了,匹配不到")
运行:
集合符号
{} 恰好指定的出现次数
例如:检查字符串是否包含“川”两个
import re txt = "川川菜鸟并不菜!"#检查字符串是否包含“川”两个x = re.findall("川{2}", txt)print(x)if x: print("匹配到了两次的川")else: print("匹配不到啊,帅哥")
返回:
或符
| 匹配两者任一
例如:匹配字符串菜鸟或者是我了
import re txt = "菜鸟们学会python了吗?串串也是菜鸟啊!"x = re.findall("菜鸟|是我了", txt)print(x)if x: print("匹配到了哦!")else: print("匹配失败")
运行:
特殊序列
指定字符
\A : 如果指定的字符位于字符串的开头,则返回匹配项。
例如:匹配以菜字符开头的字符
import re txt = "菜鸟在这里"x = re.findall("\A菜", txt)print(x)if x: print("是的匹配到了")else: print("匹配不到")
运行:
指定开头结尾
\b 返回指定字符位于单词开头或结尾的匹配项 (开头的“r”确保字符串被视为原始字符串)。
例如:匹配爱开头
import re txt = "爱你,川川"x = re.findall(r"\b爱", txt)print(x)if x: print("匹配到了")else: print("匹配不到")
运行:
又例如:匹配川结尾
import re txt = "爱你,川川"x = re.findall(r"川\b", txt)print(x)if x: print("匹配到了")else: print("匹配不到")
运行:
匹配中间字符
\B 返回存在指定字符但不在单词开头(或结尾)的匹配项 (开头的“r”确保字符串被视为“原始字符串”)
比如我匹配菜鸟:
import re txt = "我是菜鸟我是菜鸟啊"#检查是否存在“ain”,但不是在单词的开头:x = re.findall(r"\菜鸟", txt)print(x)if x: print("匹配到了嘛!!")else: print("匹配不到哇!")
运行:
但是你匹配结尾就会返回空,比如我匹配鸟:
import re txt = "川川菜鸟"#检查是否存在“鸟”,但不是在单词的末尾:x = re.findall(r"鸟\B", txt)print(x)if x: print("匹配到了哦")else: print("找不到")
运行:
匹配数字
\d 返回字符串包含数字(0-9 之间的数字)的匹配项。
例如:
import re txt = "我今年20岁了啊"#检查字符串是否包含任何位数(0-9的数字)x = re.findall("\d", txt)print(x)if x: print("哇哇哇,匹配到数字了")else: print("找不到哦")
运行:
匹配非数字
\D 返回字符串不包含数字的匹配项
例如:
import re txt = "我今年20岁"#匹配任何非数字符号x = re.findall("\D", txt)print(x)if x: print("匹配到了,开心!")else: print("匹配不到,生气")
运行:
空格匹配
\s 返回一个匹配字符串包含空白空间字符的匹配项。
例如:
import re txt = "我 是 川 川 菜 鸟"#匹配任何空格字符x = re.findall("\s", txt)print(x)if x: print("匹配到了")else: print("匹配不到啊")
运行:
匹配非空格
\S 返回字符串不包含空格字符的匹配项
import re txt = "菜鸟是 我 了"#匹配任意非空字符x = re.findall("\S", txt)print(x)if x: print("匹配到了!")else: print("匹配不到啊")
运行:
匹配任意数字和字母
返回一个匹配,其中字符串包含任何单词字符(从 a 到 Z 的字符,从 0 到 9 的数字,以及下划线 _ 字符)
例如:
import re txt = "菜鸟啊 是串串呀"#在每个单词字符(从a到z的字符,0-9的数字)返回匹配项,以及下划线_字符):x = re.findall("\w", txt)print(x)if x: print("匹配到了啊")else: print("匹配不到哇")
运行:
匹配任意非数字和字母
返回字符串不包含任何单词字符的匹配项,在每个非单词字符中返回匹配(不在A和Z之间的字符。“!”,“?”空白位等)
例如:
import re txt = "菜鸟 是 我嘛?我不信!!"#在每个非单词字符中返回匹配(不在A和Z之间的字符。“!”,“?”空白位等):x = re.findall("\W", txt)print(x)if x: print("匹配到了!")else: print("匹配不到啊")
运行:
匹配结尾
\Z 如果指定的字符位于字符串的末尾,则返回匹配项。
例如:
import re txt = "川川是菜鸟啊"x = re.findall("啊\Z", txt)print(x)if x: print("匹配到了哦!")else: print("匹配不到")
集合套装
指定符范围匹配
例如集合:[arn]
import re txt = "The rain in Spain"x = re.findall("[arn]", txt)print(x)if x: print("匹配到了!")else: print("匹配不到")
匹配任意范围内小写字母
返回任何小写字符的匹配项,按字母顺序在 a 和 n 之间。
例如:
import re txt = "hello wo r l d"x = re.findall("[a-n]", txt)print(x)if x: print("匹配到了!")else: print("匹配不到")
运行:
同样的道理,依次其它情况如下:
[^arn] 返回除 a、r 和 n 之外的任何字符的匹配项
[0123] 返回存在任何指定数字(0、1、2 或 3)的匹配项
[0-9] 返回 0 到 9 之间任意数字的匹配项
[0-5][0-9] 返回 00 到 59 中任意两位数的匹配项
[a-zA-Z] 按字母顺序返回 a 和 z 之间的任何字符的匹配,小写或大写
[+] 在集合中,+, *, ., |, (), $,{} 没有特殊含义,所以 [+] 的意思是:返回字符串中任意 + 字符的匹配项。这个我i举个例子:
import re txt = "5+6=11"#检查字符串是否有任何 + 字符:x = re.findall("[+]", txt)print(x)if x: print("匹配到了")else: print("匹配不到")
运行:
匹配对象
匹配对象是包含有关搜索和结果的信息的对象。注意:如果没有匹配,None将返回值,而不是匹配对象。
直接举个例子:
执行将返回匹配对象的搜索
import re#search() 函数返回一个 Match 对象:txt = "hello world"x = re.search("wo", txt)print(x)
运行:
Match 对象具有用于检索有关搜索和结果的信息的属性和方法:
span()返回一个包含匹配开始和结束位置的元组。 string返回传递给函数的字符串 group()返回字符串中匹配的部分
span函数
例如:打印第一个匹配项的位置(开始和结束位置)。正则表达式查找任何以大写“S”开头的单词:
import re#搜索单词开头的大写“S”字符,并打印其位置txt = "The rain in Spain"x = re.search(r"\bS\w+", txt)print(x.span())
运行:
string函数
例如:打印传递给函数的字符串
import re#返回字符串txt = "The rain in Spain"x = re.search(r"\bS\w+", txt)print(x.string)
group函数
例如:打印字符串中匹配的部分。正则表达式查找任何以大写“S”开头的单词
import re#搜索单词开头的大写“w”字符,并打印该单词:txt = "hello world"x = re.search(r"\bw\w+", txt)print(x.group())
运行:
注意:如果没有匹配,None将返回值,而不是匹配对象。
推荐学习:python教程
The above is the detailed content of Summarize and organize python regular expression analysis. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.
