当前位置:网站首页>[正则表达式] 单个字符匹配
[正则表达式] 单个字符匹配
2022-07-27 14:25:00 【柚子树cc】
1. 匹配单个字符
通过re模块能够完成使用正则表达式来匹配字符串
| 代码 | 功能 |
|---|---|
| . | 匹配任意1个字符(除了\n) |
| [ ] | 匹配[ ]中列举的字符 |
| \d | 匹配数字,即0-9 |
| \D | 匹配非数字,即不是数字 |
| \s | 匹配空白,即 空格,tab键 |
| \S | 匹配非空白 |
| \w | 匹配非特殊字符,即a-z、A-Z、0-9、_、汉字 |
| \W | 匹配特殊字符,即非字母、非数字、非汉字 |
示例1: .
import re
ret = re.match(".","M")
print(ret.group())
ret = re.match("t.o","too")
print(ret.group())
ret = re.match("t.o","two")
print(ret.group())
运行结果:
M
too
two
示例2:[]
import re
# 如果hello的首字符小写,那么正则表达式需要小写的h
ret = re.match("h","hello Python")
print(ret.group())
# 如果hello的首字符大写,那么正则表达式需要大写的H
ret = re.match("H","Hello Python")
print(ret.group())
# 大小写h都可以的情况
ret = re.match("[hH]","hello Python")
print(ret.group())
ret = re.match("[hH]","Hello Python")
print(ret.group())
ret = re.match("[hH]ello Python","Hello Python")
print(ret.group())
# 匹配0到9第一种写法
ret = re.match("[0123456789]Hello Python","7Hello Python")
print(ret.group())
# 匹配0到9第二种写法
ret = re.match("[0-9]Hello Python","7Hello Python")
print(ret.group())
ret = re.match("[0-35-9]Hello Python","7Hello Python")
print(ret.group())
# 下面这个正则不能够匹配到数字4,因此ret为None
ret = re.match("[0-35-9]Hello Python","4Hello Python")
# print(ret.group())
运行结果:
h
H
h
H
Hello Python
7Hello Python
7Hello Python
7Hello Python
示例3:\d
import re
# 普通的匹配方式
ret = re.match("嫦娥1号","嫦娥1号发射成功")
print(ret.group())
ret = re.match("嫦娥2号","嫦娥2号发射成功")
print(ret.group())
ret = re.match("嫦娥3号","嫦娥3号发射成功")
print(ret.group())
# 使用\d进行匹配
ret = re.match("嫦娥\d号","嫦娥1号发射成功")
print(ret.group())
ret = re.match("嫦娥\d号","嫦娥2号发射成功")
print(ret.group())
ret = re.match("嫦娥\d号","嫦娥3号发射成功")
print(ret.group())
运行结果:
嫦娥1号
嫦娥2号
嫦娥3号
嫦娥1号
嫦娥2号
嫦娥3号
示例4:\D
import re
match_obj = re.match("\D", "f")
if match_obj:
# 获取匹配结果
print(match_obj.group())
else:
print("匹配失败")
运行结果:
f
示例5:\s
import re
# 空格属于空白字符
match_obj = re.match("hello\sworld", "hello world")
if match_obj:
result = match_obj.group()
print(result)
else:
print("匹配失败")
# \t 属于空白字符
match_obj = re.match("hello\sworld", "hello\tworld")
if match_obj:
result = match_obj.group()
print(result)
else:
print("匹配失败")
运行结果:
hello world
hello world
示例6:\S
import re
match_obj = re.match("hello\Sworld", "hello&world")
if match_obj:
result = match_obj.group()
print(result)
else:
print("匹配失败")
match_obj = re.match("hello\Sworld", "hello$world")
if match_obj:
result = match_obj.group()
print(result)
else:
print("匹配失败")
运行结果:
hello&world
hello$world
示例7:\w
import re
# 匹配非特殊字符中的一位
match_obj = re.match("\w", "A")
if match_obj:
# 获取匹配结果
print(match_obj.group())
else:
print("匹配失败")
执行结果:
A
示例8:\W
# 匹配特殊字符中的一位
match_obj = re.match("\W", "&")
if match_obj:
# 获取匹配结果
print(match_obj.group())
else:
print("匹配失败")
执行结果:
&
边栏推荐
- JUC(JMM、Volatile)
- Spark3中Catalog组件设计和自定义扩展Catalog实现
- “router-link”各种属性解释
- MySQL interview 40 consecutive questions, interviewer, if you continue to ask, I will turn my face
- C language: factorial recursive implementation of numbers
- Unity performance optimization ----- drawcall
- js运用扩展操作符(…)简化代码,简化数组合并
- Watermelon book machine learning reading notes Chapter 1 Introduction
- DevEco Studio2.1运行项目报错
- MLX90640 红外热成像仪测温传感器模块开发笔记(七)
猜你喜欢

EMC design scheme of USB2.0 Interface

【剑指offer】面试题46:把数字翻译成字符串——动态规划

Huayun data creates a perfect information technology and innovation talent training system to help the high-quality development of information technology and innovation industry

Spark TroubleShooting整理
USB interface electromagnetic compatibility (EMC) solution

Leetcode 74. search two-dimensional matrix bisection /medium

【剑指offer】面试题50:第一个只出现一次的字符——哈希表查找

Alibaba's latest summary 2022 big factory interview real questions + comprehensive coverage of core knowledge points + detailed answers

How to edit a framework resource file separately

Watermelon book machine learning reading notes Chapter 1 Introduction
随机推荐
一文读懂鼠标滚轮事件(wheelEvent)
How to edit a framework resource file separately
Usage of countdownlatch in multithreaded environment
Discussion on STM32 power down reset PDR
Leetcode-1737- minimum number of characters to change if one of the three conditions is met
Spark 任务Task调度异常分析
Comparison of advantages and disadvantages between instrument amplifier and operational amplifier
【剑指offer】面试题55 - Ⅰ/Ⅱ:二叉树的深度/平衡二叉树
Read the wheelevent in one article
Network equipment hard core technology insider router Chapter 6 tompkinson roaming the online world (middle)
Network equipment hard core technology insider router 20 dpdk (V)
Distributed lock
Jump to the specified position when video continues playing
使用双星号代替Math.pow()
Huayun data creates a perfect information technology and innovation talent training system to help the high-quality development of information technology and innovation industry
Fluent -- layout principle and constraints
Leetcode 783. binary search tree node minimum distance tree /easy
Basic usage of kotlin
QT (five) meta object properties
How "Crazy" is Hefu Laomian, which is eager to be listed, with capital increasing frequently?