当前位置:网站首页>正则表达式:字符(2)
正则表达式:字符(2)
2022-06-29 22:44:00 【不负韶华ღ】
正则表达式是由普通字符(例如字符 a 到 z)以及特殊字符组成的文字模式。这里的特殊字符也称为元字符,对于不同的元字符,在正则表达式中表达的含义也不相同。
正则表达式的组件可以是单个字符、字符集合、字符范围、字符间的选择或者所有这些组件的任意组合。
普通字符
普通字符包括没有显式指定为元字符的所有可打印和不可打印字符。这包括所有大写和小写字母、所有数字、所有标点符号和一些其他符号。
可打印字符
| 字符 | 描述 |
|---|---|
| a-z A-Z | 匹配小写a-z A-Z |
| 0-9 | 匹配数字0-9 |
| , . ; ’ ! | 匹配标点符号, . ; ’ !等 |
# 匹配字符串中的单个字符a
>>> pattern = r"a"
>>> string = "this is a line!"
>>> re.search(pattern, string)
<re.Match object; span=(8, 9), match='a'>
# 匹配字符串中的单个数字1
>>> pattern = r"1"
>>> string = "1+2=3"
>>> re.search(pattern, string)
<re.Match object; span=(0, 1), match='1'>
# 匹配字符串中的感叹号!
>>> pattern = r"!"
>>> string = "this is a line!"
>>> re.search(pattern, string)
<re.Match object; span=(14, 15), match='!'>
非打印字符
| 字符 | 描述 |
|---|---|
| \f | 匹配一个换页符。等价于 \x0c 和 \cL。 |
| \n | 匹配一个换行符。等价于 \x0a 和 \cJ。 |
| \r | 匹配一个回车符。等价于 \x0d 和 \cM。 |
| \t | 匹配一个制表符。等价于 \x09 和 \cI。 |
| \v | 匹配一个垂直制表符。等价于 \x0b 和 \cK。 |
# 匹配一个换行符
>>> pattern = r"\n"
>>>> string = "I like python.\nI like python too."
>>> re.search(pattern, string)
<re.Match object; span=(14, 15), match='\n'>
特殊字符
特殊字符在正则表达式中有特殊的含义,如果需要匹配这些特殊字符,则需要在前面添加一个反斜杠\。比如,\\两个反斜杠匹配特殊字符\,\ *匹配特殊字符 *。
| 字符 | 描述 |
|---|---|
| \ | 将下一个字符标记为一个特殊字符、或一个原义字符、或一个 向后引用、或一个八进制转义符。 |
| . | 匹配任意字符,除了换行符。 |
| \b | 匹配一个单词边界,也就是指单词和空格间的位置。 |
| \B | 匹配非单词边界。 |
| \cx | 匹配由 x 指明的控制字符。 |
| \d | 匹配一个数字字符。等价于 [0-9]。 |
| \D | 匹配一个非数字字符。等价于 [^0-9]。 |
| \s | 匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [ \f\n\r\t\v]。 |
| \S | 匹配任何非空白字符。等价于 [^ \f\n\r\t\v]。 |
| \w | 匹配字母、数字、下划线。等价于’[A-Za-z0-9_]'。 |
| \W | 匹配非字母、数字、下划线。等价于 ‘[^A-Za-z0-9_]’。 |
| ^ | 匹配输入字符串的开始位置。 |
| $ | 匹配输入字符串的结束位置。 |
| a|b | 匹配a或b。 |
| ( ) | 匹配括号内的表达式,也表示一个组。 |
| \1…\9 | 匹配第n个分组的内容。 |
| [xyz] | 字符集合。匹配所包含的任意一个字符。 |
| [^xyz] | 负值字符集合。匹配未包含的任意字符。 |
| [a-z] | 字符范围。匹配指定范围内的任意字符。 |
| [^a-z] | 负值字符范围。匹配任何不在指定范围内的任意字符。 |
| * | 匹配前面的子表达式零次或多次。 |
| + | 匹配前面的子表达式一次或多次。 |
| ? | 匹配前面的子表达式零次或一次。 |
| {n} | n 是一个非负整数。匹配确定的 n 次。 |
| {n,} | n 是一个非负整数。至少匹配n 次。 |
| {n,m} | m 和 n 均为非负整数,其中n <= m。最少匹配 n 次且最多匹配 m 次。 |
# 匹配所有的数字
>>> string = "123abc456def"
>>> pattern = r"\d+"
>>> re.findall(pattern, string)
['123', '456']
# 匹配组ab出现两次以上的字符串
>>> pattern = r"(ab){2,}"
>>> string = "abcabcababab"
>>> re.search(pattern, string)
<re.Match object; span=(6, 12), match='ababab'>
# 匹配在集合a, b, c中且出现一次以上的字符串
>>> pattern = r"[abc]{1,}"
>>> string = "bcdfghijkl"
>>> re.search(pattern, string)
<re.Match object; span=(0, 2), match='bc'>
>>> pattern = r"[abc]+"
# 用+来代替{1,},结果等价
>>> re.search(pattern, string)
>>> string = "bcdfghijkl"
<re.Match object; span=(0, 2), match='bc'>
下面来看一个邮箱匹配的例子:
# 邮箱号匹配
>>> pattern = r"\b[\w.%+-][email protected][\w.-]+\.[a-zA-Z]{2,6}\b"
>>> string = "[email protected]"
>>> re.search(pattern, string)
<re.Match object; span=(0, 14), match='[email protected]'>

边栏推荐
- 啃下大骨头——排序(一)
- 5-2web application vulnerability scanning
- Discussion on distributed unique ID generation scheme
- 深入解析kubernetes中的选举机制
- mysql备份数据库linux
- What if MySQL fails to store emoticons
- The details of industry are all made by money and time
- Qdomdocument and qdomnode are used in QT to read XML
- constexpr 函数
- MySQL 锁常见知识点&面试题总结
猜你喜欢

深入解析kubernetes controller-runtime
![Realizing deep learning framework from zero -- LSTM from theory to practice [theory]](/img/ac/164140eff1a6518d49ce25599d9c7b.png)
Realizing deep learning framework from zero -- LSTM from theory to practice [theory]

Low code, end-to-end, one hour to build IOT sample scenarios, and the sound network released lingfalcon Internet of things cloud platform
Why does copying files on a shared folder on a local area network (ERP server) result in the loss of the local Internet

wirehark数据分析与取证infiltration.pacapng

Daily mathematics serial 54: February 23

开源了 | 文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开

Open source the Ernie tiny lightweight technology of "Wenxin big model", which is accurate and fast, with full effect

《天天数学》连载54:二月二十三日

字节云数据库未来方向的探索与实践
随机推荐
SYSTEMd debugging
数据库-玩转数据-Pgsql 使用UUID做主键
Qt5.14.2 error connecting to the MySQL database of Ubuntu 20.04
Hidden worries behind the listing of shushulang: the performance has declined significantly, the market position is relatively backward, and the competitiveness is questionable
GWD: rotating target detection based on Gaussian Wasserstein distance | ICML 2021
MySQL backup database Linux
把数组排成最小的数_数组中的逆序对(归并统计法)_数字在升序数组中出现的次数_丑数(剑指offer)
The details of industry are all made by money and time
Kr-gcn: an interpretable recommendation system based on knowledge aware reasoning
Talk about auto in MySQL in detail_ What is the function of increment
The client can connect to remote MySQL
Digital tracking analysis of insurance services in the first quarter of 2022
Efficient implementation of dynamiccast with template function and specialization function
在线文本数字识别列表求和工具
NRM explanation
股票开户安全吗?上海股票开户。
One click file sharing software jirafeau
Qt中使用QDomDocument和QDomnode来读取xml
Unicom warehousing | all Unicom companies that need to sell their products need to enter the general warehouse first
wirehark数据分析与取证infiltration.pacapng