2021-02-27 15:01:45 +00:00
|
|
|
|
# 原创
|
|
|
|
|
: Python正则表达式(一)
|
|
|
|
|
|
|
|
|
|
# Python正则表达式(一)
|
|
|
|
|
|
2024-07-03 09:49:47 +00:00
|
|
|
|
## 正则表达式
|
2021-02-27 15:01:45 +00:00
|
|
|
|
|
|
|
|
|
>
|
|
|
|
|
参考:[正则表达式](https://github.com/CyC2018/Interview-Notebook/blob/master/notes/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F.md)
|
|
|
|
|
|
|
|
|
|
|
2024-07-03 09:49:47 +00:00
|
|
|
|
### 一、概述
|
2021-02-27 15:01:45 +00:00
|
|
|
|
|
2024-07-03 09:49:47 +00:00
|
|
|
|
### 二、匹配单个字符
|
2021-02-27 15:01:45 +00:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
# 正则表达式
|
|
|
|
|
nam.
|
|
|
|
|
# 匹配结果
|
|
|
|
|
My name is DS中的name
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2024-07-03 09:49:47 +00:00
|
|
|
|
### 三、匹配一组字符串
|
2021-02-27 15:01:45 +00:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
abcd
|
|
|
|
|
abc1
|
|
|
|
|
abc2
|
|
|
|
|
# 匹配到了abcd
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2024-07-03 09:49:47 +00:00
|
|
|
|
### 四、使用元字符
|
2021-02-27 15:01:45 +00:00
|
|
|
|
|
2024-07-03 09:49:47 +00:00
|
|
|
|
#### 匹配空白字符
|
2021-02-27 15:01:45 +00:00
|
|
|
|
|
|
|
|
|
<th align="center">元字符</th><th align="center">说明</th>
|
|
|
|
|
|------
|
|
|
|
|
<td align="center">[\b]</td><td align="center">回退(删除一个字符)</td>
|
|
|
|
|
<td align="center">\f</td><td align="center">换页符</td>
|
|
|
|
|
<td align="center">\n</td><td align="center">换行符</td>
|
|
|
|
|
<td align="center">\r</td><td align="center">回车符</td>
|
|
|
|
|
<td align="center">\t</td><td align="center">制表符</td>
|
|
|
|
|
<td align="center">\v</td><td align="center">垂直制表符</td>
|
|
|
|
|
|
2024-07-03 09:49:47 +00:00
|
|
|
|
#### 匹配特定的字符类别
|
2021-02-27 15:01:45 +00:00
|
|
|
|
|
2024-07-03 09:49:47 +00:00
|
|
|
|
##### 数字元字符
|
2021-02-27 15:01:45 +00:00
|
|
|
|
|
|
|
|
|
<th align="center">元字符</th><th align="center">说明</th>
|
|
|
|
|
|------
|
|
|
|
|
<td align="center">\d</td><td align="center">数字字符,等价于[0-9]</td>
|
|
|
|
|
<td align="center">\D</td><td align="center">非数字字符,等价于[^0-9]</td>
|
|
|
|
|
|
2024-07-03 09:49:47 +00:00
|
|
|
|
##### 字母数字元字符
|
2021-02-27 15:01:45 +00:00
|
|
|
|
|
|
|
|
|
<th align="center">元字符</th><th align="center">说明</th>
|
|
|
|
|
|------
|
|
|
|
|
<td align="center">\w</td><td align="center">大小写字母,下划线和数字,等价于[a-zA-Z0-9]</td>
|
|
|
|
|
<td align="center">\W</td><td align="center">对\w取非</td>
|
|
|
|
|
|
2024-07-03 09:49:47 +00:00
|
|
|
|
##### 空白字符元字符
|
2021-02-27 15:01:45 +00:00
|
|
|
|
|
|
|
|
|
<th align="center">元字符</th><th align="center">说明</th>
|
|
|
|
|
|------
|
|
|
|
|
<td align="center">\s</td><td align="center">任何一个空白字符,等价于[\f\n\r\t\v]</td>
|
|
|
|
|
<td align="center">\S</td><td align="center">对\s取非</td>
|
|
|
|
|
|
2024-07-03 09:49:47 +00:00
|
|
|
|
### 五、重复匹配
|
2021-02-27 15:01:45 +00:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
[\w.]+@\w+\.\w+
|
|
|
|
|
[\w.]+@[\w]+[\.][\w]+
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2024-07-03 09:49:47 +00:00
|
|
|
|
### 六、位置匹配
|
2021-02-27 15:01:45 +00:00
|
|
|
|
|
2024-07-03 09:49:47 +00:00
|
|
|
|
#### 单词边界
|
2021-02-27 15:01:45 +00:00
|
|
|
|
|
2024-07-03 09:49:47 +00:00
|
|
|
|
#### 字符串边界
|
2021-02-27 15:01:45 +00:00
|
|
|
|
|
2024-07-03 09:49:47 +00:00
|
|
|
|
### 七、使用子表达式
|
2021-02-27 15:01:45 +00:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
192.168.0.1
|
|
|
|
|
00.00.00.00
|
|
|
|
|
555.555.555.555
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
匹配到了第一个
|
|
|
|
|
|
2024-07-03 09:49:47 +00:00
|
|
|
|
### 八、回溯引用
|
2021-02-27 15:01:45 +00:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
<h1>x</h1>
|
|
|
|
|
<h2>x</h2>
|
|
|
|
|
<h3>x</h1>
|
|
|
|
|
匹配到了前面两个
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
<th align="center">元字符</th><th align="center">说明</th>
|
|
|
|
|
|------
|
|
|
|
|
<td align="center">\l</td><td align="center">把下个字符转换成小写</td>
|
|
|
|
|
<td align="center">\u</td><td align="center">把下个字符转换成大写</td>
|
|
|
|
|
<td align="center">\L</td><td align="center">把\L和\E之间的字符全部转换成小写</td>
|
|
|
|
|
<td align="center">\U</td><td align="center">把\U和\E之间的字符全部转换成大写</td>
|
|
|
|
|
<td align="center">\E</td><td align="center">结束\L或者\U</td>
|
|
|
|
|
|
2024-07-03 09:49:47 +00:00
|
|
|
|
### 九、前后查找
|
2021-02-27 15:01:45 +00:00
|
|
|
|
|
2024-07-03 09:49:47 +00:00
|
|
|
|
### 十、嵌入条件
|
2021-02-27 15:01:45 +00:00
|
|
|
|
|
2024-07-03 09:49:47 +00:00
|
|
|
|
#### 回溯引用条件
|
2021-02-27 15:01:45 +00:00
|
|
|
|
|
2024-07-03 09:49:47 +00:00
|
|
|
|
#### 前后查找条件
|