当前位置:网站首页>05 regular expression syntax
05 regular expression syntax
2022-07-21 19:18:00 【Yinma Hanhai】
One 、 Regular expression common Syntax
. : Is any character except newline a.c —> You can talk to abc matching 、acc matching 、a$c matching
****: It's the escape sign ,a.c–>a.c a\c—>a\c
[…]: Indicates that any character appears only once
\d: Is the match 0–9 One of the numbers
\D: Match non numeric , As long as it's not a number
\s: It is used to match white space a\sc–> a c
\S: It is used to match non blank characters a\Sc–>abc
\w: Match all the letters ( Include case ) Numbers and underscores and Chinese
\W: As long as it's not a letter ( Include case ) Numbers and underscores and Chinese , Everything else can match
** * *: Indicates the occurrence of the previous character 0 Times or times \d
+: Indicates that the previous character appears one or more times ( At least there must be 1 Time )
?: Express Match the previous character 0 Time or 1 Time . Can only match to the front 1 individual
{m}: Match the previous character m Time
# 1、 Import the regular module
import re
# 2、 Character matching
# One 、 Simple string matching
rs = re.findall('abc', 'abc') # The first one is regular expression , Next is the string we are looking for
print(rs) # ['abc']
rs = re.findall('abc', 'abcfeffevfefggh') # Find the content we need from the following string
print(rs) # ['abc']
# Two 、 Use “.” Match (. Is to match any character except newline , Just can't match newline )
rs = re.findall('a.c', 'abc')
print(rs) # ['abc']
rs = re.findall('a.c', 'a%c')
print(rs) # ['a%c']
rs = re.findall('a.c', 'a\nc')
print(rs) # [] Just can't match newline
rs = re.findall('a\.c', 'a.c') # Only match a.c
print(rs) # ['a.c']
rs = re.findall('a\.c', 'abc') # Only match a.c
print(rs) # [] Only match a.c
# 3、 ... and 、 Use “[]” Match
rs = re.findall('a[bc]d', 'abd')
print(rs) # ['abd']
rs = re.findall('a[bc]d', 'acd')
print(rs) # ['acd']
rs = re.findall('a[bc]d', 'aed')
print(rs) # [] Only match abd、abc
# Four 、 Predefined character set
rs = re.findall('\d', '123')
print(rs) # ['1', '2', '3']
rs = re.findall('\w', '123_ chinese ')
print(rs) # ['1', '2', '3', '_', ' in ', ' writing ']
rs = re.findall('\w', '123_ chinese $')
print(rs) # ['1', '2', '3', '_', ' in ', ' writing '] Mismatch $ Symbol
rs = re.findall('\W', '123_ chinese $')
print(rs) # ['$']
# 5、 ... and 、 quantifiers
rs = re.findall('\d*', '123') # Empty strings are allowed
print(rs) # ['123', '']
rs = re.findall('a\d*', 'a123')
print(rs) # ['a123']
rs = re.findall('\d+', '123')
print(rs) # ['123']
rs = re.findall('a\d?', 'a123')
print(rs) # ['a1']
rs = re.findall('a\d{2}', 'a123')
print(rs) # ['a12']
Two 、findall() Method
flag It's for the right . and \n Matching
import re
# 1.findall Method , Return a list of matching results
rs = re.findall('\d+', 'chuan13zhi24')
print(rs)
# 2.findall In the method ,flag The role of
rs = re.findall('a.bc', 'a\nbc') # . Cannot match newline
print(rs) # []
# If you want to . It can match the newline character , We need to use flag This parameter .re.DOTALL perhaps re.S such , You can match all characters
rs = re.findall('a.bc', 'a\nbc', re.DOTALL)
print(rs) # ['a\nbc']
rs = re.findall('a.bc', 'a\nbc', re.S)
print(rs) # ['a\nbc']
# 3.findall Method , Grouping is existence ()
''' About the use of grouping in regularity , If there is no grouping , It is to use the whole regular string to find the content that matches this regular , Then go back . however , If this findall Regular expressions in methods have groupings , That is, if there are parentheses , Then it will only return the content that matches the rule in the parentheses , Will return , such as : a(.+)bc, Only returned \n. Other regularities are used for positioning . Only here a hinder ,bc Previous content , We don't want any other places '''
# No grouping , Then match from the whole string
rs = re.findall('a.+bc', 'a\nbc', re.S)
print(rs) # ['a\nbc']
# This is grouping ( There are parentheses ), Only look for a hinder b Previous content ,a and b It's for positioning
rs = re.findall('a(.+)bc', 'a\nbc', re.S)
print(rs) # ['a\nbc']
3、 ... and 、r Use in the original string
import re
'''re The role of : There are several \ Just a few \, In this way, it is very simple for us to write regular expressions '''
# 1、 Without using r When the original string , Encountered escape character (\) How do you do it?
rs=re.findall('a\nbc','a\nbc')
print(rs)
rs=re.findall('a\\nbc','a\\nbc')
print(rs)
rs=re.findall('a\\\nbc','a\\nbc')
print(rs)
rs=re.findall('a\\\\nbc','a\\nbc')
print(rs) # ['a\\nbc'] Need to write 4 Escape characters
# r The original string in the regular can eliminate the influence of escape characters
rs=re.findall(r'a\\nbc','a\\nbc')
print(rs) # ['a\\nbc']
rs=re.findall(r'a\nbc','a\nbc')
print(rs) # ['a\nbc']
# Expand : It can solve the problem of writing regular , Do not conform to the PEP8 The problem of specification ( No, r It's OK )
rs=re.findall(r'\d','a123')
print(rs) # ['1', '2', '3']
边栏推荐
- C语言进阶(十四) - 文件管理
- toast_tuple_threshold的疑问
- PyTorch 深度学习实践 第2讲/作业(Linear Model)
- SuperMap iClient for OpenLayers图层组控制实现方法
- 软件测试面试题:测试计划工作的目的是什么?测试计划文档的内容应该包括什么?其中哪些是最重要的?
- 2022 software testing skills jmeter+ant+jenkins continuous integration and test report generation tutorial
- Advanced C language (XIV) - Document Management
- 5. Customize global AuthenticationManager
- Software test interview question: in your previous work, what did a software defect (or bug) record contain? How to submit high-quality software bug records?
- 05 正则表达式语法
猜你喜欢
NFT: the core of digital ownership
Phpstudy_pro搭建Sqli-labs靶场,进行SQL注入测试
TiDB 分布式批量解决方案
Essential tools for streamlit Data Science
FTP服務配置
Configuration du Service FTP
Ardunio development - I2C protocol communication - control 2x16lcd
NFS service configuration
Phpstudy_ Pro builds sqli labs shooting range for SQL injection test
Pytorch环境搭建
随机推荐
[information retrieval] implementation of information retrieval system
Don't want to wake up because it's delicious
Advanced C language - static address book
使用MogDB Operator在Kubernetes上部署MogDB集群(MogDB Stack)
软件测试面试题:测试人员在软件开发过程中的任务是什么?
软件测试面试题:简述你在以前的工作中做过哪些事情,比较熟悉什么。参考答案如下。
MYSQL09_精讲数据库数据类型
线程池.线程数量设置
Software testing interview questions: black box testing and white box testing are two basic methods of software testing. Please explain their advantages and disadvantages respectively!
【性能优化】MySQL常用慢查询分析工具
【QT】日志路径的字符编码转换正确方式
[QT入门篇]定时器QTimer类
Spark RDD, application case of spark SQL
分布式.数据库架构
[QT] correct method of character encoding conversion of log path
2022年互联网大厂Android高频面试题及答案及知识脉络的整理
Software test interview question: in your previous work, what did a software defect (or bug) record contain? How to submit high-quality software bug records?
Emuelec Development Notes
强制卸载额外域控制器
What is gamefi?