-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython-regular-expression.html
189 lines (142 loc) · 126 KB
/
python-regular-expression.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<!DOCTYPE html><html><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><title>Python正则表达式 - Dylan</title><meta name="keywords" content="Python,正则"><meta name="description" content=" 为什么使用正则表达式?
典型的搜索和替换操作要求您提供与预期的搜索结果匹配的确切文本。虽然这种技术对于对静态文本执行简单搜索和替换任务可能已经足够了,但它缺乏灵活性,若采用这种方法搜索动态文本,即使不是不可能,至少也会变得很困难。
通过使用正则表达式,可以:
测试字符串内的模式。
例如,可以测试输入字符串,以查看字符串内是否出现电话号码模式或信用卡号码模式。这称为数据验证。
替换文本。
可以使用正则表达式来识别文档中的特定文本,完全删除该文本或者用其他文本替换它。
基于模式匹配从字符串中提取子字符串。
可以查找文档内或输入域内特定的文本。
"><meta name="HandheldFriendly" content="True"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1"><meta name="baidu-site-verification" content="Ar3EqWfUYV"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/dxsm/cdn/lib/font-awesome/css/font-awesome.min.css"><link rel="shortcut icon" type="image/x-icon" href="https://fastly.jsdelivr.net/gh/dxsm/cdn/images/favicon.ico"><link rel="stylesheet" href="https://fastly.jsdelivr.net/gh/dxsm/[email protected]/css/style.css"><script>function setLoadingBarProgress(e){document.getElementById("loading-bar").style.width=e+"%"}</script><style>#loading-container{position:fixed;top:0;left:0;min-height:100vh;width:100vw;z-index:9999;display:flex;flex-direction:column;justify-content:center;align-items:center;background:#fff;text-align:center;-webkit-transition:opacity 1s ease;-moz-transition:opacity 1s ease;-o-transition:opacity 1s ease;transition:opacity 1s ease}.loading-image{width:120px;height:50px;transform:translate(-50%)}.loading-image div:nth-child(2){-webkit-animation:pacman-balls 1s linear 0s infinite;animation:pacman-balls 1s linear 0s infinite}.loading-image div:nth-child(3){-webkit-animation:pacman-balls 1s linear .33s infinite;animation:pacman-balls 1s linear .33s infinite}.loading-image div:nth-child(4){-webkit-animation:pacman-balls 1s linear .66s infinite;animation:pacman-balls 1s linear .66s infinite}.loading-image div:nth-child(5){-webkit-animation:pacman-balls 1s linear .99s infinite;animation:pacman-balls 1s linear .99s infinite}.loading-image div:first-of-type{width:0;height:0;border:25px solid #49b1f5;border-right-color:transparent;border-radius:25px;-webkit-animation:rotate_pacman_half_up .5s 0s infinite;animation:rotate_pacman_half_up .5s 0s infinite}.loading-image div:nth-child(2){width:0;height:0;border:25px solid #49b1f5;border-right-color:transparent;border-radius:25px;-webkit-animation:rotate_pacman_half_down .5s 0s infinite;animation:rotate_pacman_half_down .5s 0s infinite;margin-top:-50px}@-webkit-keyframes rotate_pacman_half_up{0%{transform:rotate(270deg)}50%{transform:rotate(1turn)}to{transform:rotate(270deg)}}@keyframes rotate_pacman_half_up{0%{transform:rotate(270deg)}50%{transform:rotate(1turn)}to{transform:rotate(270deg)}}@-webkit-keyframes rotate_pacman_half_down{0%{transform:rotate(90deg)}50%{transform:rotate(0)}to{transform:rotate(90deg)}}@keyframes rotate_pacman_half_down{0%{transform:rotate(90deg)}50%{transform:rotate(0)}to{transform:rotate(90deg)}}@-webkit-keyframes pacman-balls{75%{opacity:.7}to{transform:translate(-100px,-6.25px)}}@keyframes pacman-balls{75%{opacity:.7}to{transform:translate(-100px,-6.25px)}}.loading-image div:nth-child(3),.loading-image div:nth-child(4),.loading-image div:nth-child(5),.loading-image div:nth-child(6){background-color:#49b1f5;width:15px;height:15px;border-radius:100%;margin:2px;width:10px;height:10px;position:absolute;transform:translateY(-6.25px);top:25px;left:100px}.loading-text{margin-bottom:10vh;text-align:center;color:#2c3e50;font-size:2rem;box-sizing:border-box;padding:0 10px;text-shadow:0 2px 10px rgba(0,0,0,.2)}@media only screen and (max-width:500px){.loading-text{font-size:1.5rem}.loading-image{width:90px}}.fadeout{opacity:0}@-webkit-keyframes fadeInDown{0%{opacity:0;-webkit-transform:translate3d(0,-100%,0);transform:translate3d(0,-100%,0)}100%{opacity:1;-webkit-transform:none;transform:none}}@keyframes fadeInDown{0%{opacity:0;-webkit-transform:translate3d(0,-100%,0)}}</style><script>(function () {
const loaded = function(){
setTimeout(function(){
const loader = document.getElementById("loading-container");
loader.className="fadeout" ;//使用渐隐的方法淡出loading page
// document.getElementById("body-wrap").style.display="flex";
setTimeout(function(){
loader.style.display="none";
},1000);
},1000); //强制显示loading page 1s
};
loaded();
})()</script><meta name="generator" content="Hexo 5.4.0"></head><body><div id="loading-bar-wrapper"><div id="loading-bar" class="pure"></div></div><div id="loading-container"><p class="loading-text">玩命加载中 . . .</p><div class="loading-image"><div></div><div></div><div></div><div></div><div></div></div></div><script>setLoadingBarProgress(20)</script><header class="l_header pure"><div class="wrapper"><div class="nav-main container container--flex"><a class="logo flat-box" href="/">Dylan</a><div class="menu"><ul class="h-list"><li><a id="home" class="nav flat-box" href="/" target=""><i class="fa fa-home fa-fw"></i> 主页</a></li><li><a id="archives" class="nav flat-box" href="/archives/" target=""><i class="fa fa-archive fa-fw"></i> 归档</a></li><li><a id="listbooks.html" class="nav flat-box" href="/list/books.html" target=""><i class="fa fa-book fa-fw"></i> 书籍</a></li><li><a id="listtools.html" class="nav flat-box" href="/list/tools.html" target=""><i class="fa fa-wrench fa-fw"></i> 工具</a></li><li><a id="listgames.html" class="nav flat-box" href="/list/games.html" target=""><i class="fa fa-cubes fa-fw"></i> 娱乐</a></li><li><a id="box" class="nav flat-box" href="/box/" target="__blank"><i class="fa fa-flask fa-fw"></i> 百宝箱</a></li><li><a id="links" class="nav flat-box" href="/links/" target=""><i class="fa fa-link fa-fw"></i> 友链</a></li><li><a id="contact" class="nav flat-box" href="/contact/" target=""><i class="fa fa-comments-o fa-fw"></i> 留言版</a></li></ul></div><div class="m_search"><form name="searchform" class="form u-search-form"><input type="text" class="input u-search-input" placeholder="搜索"><span class="icon"><i class="fa fa-search fa-fw"></i></span></form></div><ul class="switcher h-list"><li class="s-search"><a class="fa fa-search fa-fw" href="javascript:void(0)"></a></li><li class="s-menu"><a class="fa fa-bars fa-fw" href="javascript:void(0)"></a></li></ul></div><div class="nav-sub container container--flex"><a class="logo flat-box"></a><ul class="switcher h-list"><li class="s-comment"><a class="flat-btn fa fa-comments fa-fw" href="javascript:void(0)"></a></li><li class="s-toc"><a class="flat-btn fa fa-list-ul fa-fw" href="javascript:void(0)"></a></li></ul></div></div></header><aside class="menu-phone"><section class="author" style="background-image:url(https://fastly.jsdelivr.net/gh/dxsm/cdn/images/nav-bg.jpg)"><div class="avatar-wrap"><div class="avatar" style="background-image:url(https://fastly.jsdelivr.net/gh/dxsm/cdn/images/avatar.png)"></div><span class="name">董续胜</span></div><p class="info">记录时光,走向成长</p></section><header><nav class="menu"><ul><li><a id="home" class="nav flat-box" href="/"><i class="fa fa-home fa-fw"></i> 主页</a></li><li><a id="archives" class="nav flat-box" href="/archives/"><i class="fa fa-archive fa-fw"></i> 归档</a></li><li><a id="categories" class="nav flat-box" href="/categories/"><i class="fa fa-th fa-fw"></i> 分类</a></li><li><a id="tags" class="nav flat-box" href="/tags/"><i class="fa fa-tags fa-fw"></i> 标签</a></li><li><a id="listbooks.html" class="nav flat-box" href="/list/books.html"><i class="fa fa-book fa-fw"></i> 书籍</a></li><li><a id="listtools.html" class="nav flat-box" href="/list/tools.html"><i class="fa fa-wrench fa-fw"></i> 工具</a></li><li><a id="listgames.html" class="nav flat-box" href="/list/games.html"><i class="fa fa-cubes fa-fw"></i> 娱乐</a></li><li><a id="box" class="nav flat-box" href="/box/"><i class="fa fa-flask fa-fw"></i> 百宝箱</a></li><li><a id="links" class="nav flat-box" href="/links/"><i class="fa fa-link fa-fw"></i> 友链</a></li><li><a id="contact" class="nav flat-box" href="/contact/"><i class="fa fa-comments-o fa-fw"></i> 留言版</a></li></ul></nav></header><hr><div class="social-wrapper"><a href="http://www.uestc.edu.cn/" title="2016年毕业于电子科技大学" class="social flat-btn" target="_blank" rel="external nofollow"><i class="social fa fa-university fa_fw" aria-hidden="true"></i></a><a href="http://gr.uestc.edu.cn/" title="硕士学历" class="social flat-btn" target="_blank" rel="external nofollow"><i class="social fa fa-graduation-cap fa_fw" aria-hidden="true"></i></a><a href="#" title="中国 上海" class="social flat-btn" target="_blank" rel="external nofollow"><i class="social fa fa-map-marker fa_fw" aria-hidden="true"></i></a><a href="#" title="QQ: 959229640" class="social flat-btn" target="_blank" rel="external nofollow"><i class="social fa fa-qq fa_fw" aria-hidden="true"></i></a><a href="mailto:[email protected]" title="[email protected]" class="social flat-btn" target="_blank" rel="external nofollow"><i class="social fa fa-envelope fa_fw" aria-hidden="true"></i></a></div></aside><script>setLoadingBarProgress(40)</script><div class="l_body"><div class="container clearfix"><div class="l_main"><article id="post" class="post white-box article-type-post" itemscope itemprop="blogPost"><section class="meta"><div class="meta" id="header-meta"><h1 class="title">Python正则表达式</h1><div class="new-meta-box"><div class="new-meta-item category"><a href="/categories/Python/"><i class="fa fa-folder-o" aria-hidden="true"></i> Python</a></div></div><hr></div></section><section class="article typo"><div class="article-entry" itemprop="articleBody"><h2 id="wei-shi-me-shi-yong-zheng-ze-biao-da-shi"><a class="markdownIt-Anchor" href="#wei-shi-me-shi-yong-zheng-ze-biao-da-shi"></a> 为什么使用正则表达式?</h2><p>典型的搜索和替换操作要求您提供与预期的搜索结果匹配的确切文本。虽然这种技术对于对静态文本执行简单搜索和替换任务可能已经足够了,但它缺乏灵活性,若采用这种方法搜索动态文本,即使不是不可能,至少也会变得很困难。</p><p>通过使用正则表达式,可以:</p><ul data-mark="-"><li><strong>测试字符串内的模式。</strong><br>例如,可以测试输入字符串,以查看字符串内是否出现电话号码模式或信用卡号码模式。这称为数据验证。</li><li><strong>替换文本</strong>。<br>可以使用正则表达式来识别文档中的特定文本,完全删除该文本或者用其他文本替换它。</li><li><strong>基于模式匹配从字符串中提取子字符串。</strong><br>可以查找文档内或输入域内特定的文本。</li></ul><span id="more"></span><h2 id="pi-pei-gui-ze"><a class="markdownIt-Anchor" href="#pi-pei-gui-ze"></a> 匹配规则</h2><p><img src="https://cdn.jsdelivr.net/gh/dxsm/cdn/images/loading.svg" alt="" data-echo="https://cdn.jsdelivr.net/gh/dxsm/note@latest/assets/markdown-img-paste-20190824164005821.png"></p><h2 id="re-mo-kuai-chang-yong-han-shu"><a class="markdownIt-Anchor" href="#re-mo-kuai-chang-yong-han-shu"></a> re模块常用函数</h2><h3 id="rematch-han-shu"><a class="markdownIt-Anchor" href="#rematch-han-shu"></a> re.match函数</h3><p>re.match尝试从字符串的起始位置匹配一个模式,匹配成功则返回match对象;如果不是起始位置匹配成功的话,match()就返回none。</p><p><strong>函数语法:</strong></p><div class="prism-hl"><div class="code-tool"><div class="code-show" type="hide"><div class="show"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647 802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329L734.306083 294.306329 734.306083 294.306329C716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214L289.779333 293.88214 289.779333 293.88214C272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L221.990112 290.329458 16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231Z" id="Combined-Shape"></path></g></svg></div><div class="hide"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329 716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214 272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231ZM649.492098 134.243566C674.403037 143.310407 687.247217 170.85484 678.180377 195.765779L436.030115 861.068155C426.963275 885.979094 399.418842 898.823274 374.507902 889.756434 349.596963 880.689593 336.752783 853.14516 345.819623 828.234221L587.969885 162.931845 587.969885 162.931845C597.036725 138.020906 624.581158 125.176726 649.492098 134.243566Z" id="Combined-Shape"></path></g></svg></div></div><div class="copy" aria-label=""><svg preserveAspectRatio="xMidYMid meet" fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor" class="icon-7f6730be--text-3f89f380"><g><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></g></svg></div></div><pre data_role="codeBlock" class="language-python">re<span class="token punctuation">.</span>match<span class="token punctuation">(</span>pattern<span class="token punctuation">,</span> string<span class="token punctuation">[</span><span class="token punctuation">,</span> flags<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</pre></div><p><strong>函数参数说明:</strong></p><ul data-mark="-"><li>pattern:匹配的正则表达式</li><li>string:要匹配的字符串。</li><li>flags:标志位,用于控制正则表达式的匹配方式,取值可以使用按位或运算符’|'表示同时生效<ul data-mark="*"><li>re.I(re.IGNORECASE):忽略大小写</li><li>re.M(re.MULTILINE):多行匹配,改变’^‘和’$'的行为</li><li>re.S(re.DOTALL):改变’.‘的行为,使’.'匹配包括换行在内的所有字符</li><li>re.L(re.LOCALE):做本地化识别(locale-aware)匹配,使预定字符类 \w \W \b \B \s \S 取决于当前区域设定</li><li>re.U(re.UNICODE):根据Unicode字符集解析字符。这个标志影响 \w, \W, \b, \B.</li><li>re.X(re.VERBOSE): 详细模式。这个模式下正则表达式可以是多行,忽略空白字符,并可以加入注释。</li></ul></li></ul><h4 id="fan-hui-zhi-match-dui-xiang"><a class="markdownIt-Anchor" href="#fan-hui-zhi-match-dui-xiang"></a> 返回值match对象</h4><p>Match对象是一次匹配的结果,包含了很多关于此次匹配的信息,可以使用Match提供的可读属性或方法来获取这些信息。</p><p><strong>属性:</strong></p><ul data-mark="*"><li>string: 匹配时使用的文本。</li><li>re: 匹配时使用的Pattern对象。</li><li>pos: 文本中正则表达式开始搜索的索引。值与Pattern.match()和Pattern.search()方法的同名参数相同。</li><li>endpos: 文本中正则表达式结束搜索的索引。值与Pattern.match()和Pattern.search()方法的同名参数相同。</li><li>lastindex: 最后一个被捕获的分组在文本中的索引。如果没有被捕获的分组,将为None。</li><li>lastgroup: 最后一个被捕获的分组的别名。如果这个分组没有别名或者没有被捕获的分组,将为None。</li></ul><p><strong>方法:</strong></p><ul data-mark="*"><li>group([group1, …]):<br>获得一个或多个分组截获的字符串;<strong>指定多个参数时将以元组形式返回</strong>。group1可以使用编号也可以使用别名;编号0代表整个匹配的子串;不填写参数时,返回group(0);没有截获字符串的组返回None;截获了多次的组返回最后一次截获的子串。</li><li>groups([default]):<br>以元组形式返回全部分组截获的字符串。相当于调用group(1,2,…last)。default表示没有截获字符串的组以这个值替代,默认为None。</li><li>groupdict([default]):<br>返回以有别名的组的别名为键、以该组截获的子串为值的字典,<strong>没有别名的组不包含在内</strong>。default含义同上。</li><li>start([group]):<br>返回指定的组截获的子串在string中的起始索引(子串第一个字符的索引)。group默认值为0。</li><li>end([group]):<br>返回指定的组截获的子串在string中的结束索引(子串最后一个字符的索引+1)。group默认值为0。</li><li>span([group]):<br>返回(start(group), end(group))。</li><li>expand(template):<br>将匹配到的分组代入template中然后返回。template中可以使用\id或\g<id>、\g<name>引用分组,<strong>但不能使用编号0</strong>。\id与\g<id>是等价的;但\10将被认为是第10个分组,如果你想表达\1之后是字符’0’,而不是分组10,只能使用\g<1>0。</id></name></id></li></ul><h4 id="shi-li"><a class="markdownIt-Anchor" href="#shi-li"></a> 实例</h4><div class="prism-hl"><div class="code-tool"><div class="code-show" type="hide"><div class="show"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647 802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329L734.306083 294.306329 734.306083 294.306329C716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214L289.779333 293.88214 289.779333 293.88214C272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L221.990112 290.329458 16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231Z" id="Combined-Shape"></path></g></svg></div><div class="hide"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329 716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214 272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231ZM649.492098 134.243566C674.403037 143.310407 687.247217 170.85484 678.180377 195.765779L436.030115 861.068155C426.963275 885.979094 399.418842 898.823274 374.507902 889.756434 349.596963 880.689593 336.752783 853.14516 345.819623 828.234221L587.969885 162.931845 587.969885 162.931845C597.036725 138.020906 624.581158 125.176726 649.492098 134.243566Z" id="Combined-Shape"></path></g></svg></div></div><div class="copy" aria-label=""><svg preserveAspectRatio="xMidYMid meet" fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor" class="icon-7f6730be--text-3f89f380"><g><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></g></svg></div></div><pre data_role="codeBlock" class="language-python line-numbers"><span class="token comment">#!/usr/bin/env python</span>
<span class="token keyword">import</span> re
str1 <span class="token operator">=</span> <span class="token triple-quoted-string string">"""baidu.com
www.google.com
www.12306.cn"""</span>
str2 <span class="token operator">=</span> <span class="token triple-quoted-string string">"""www.baidu.com
www.google.com
www.12306.cn"""</span>
pattern <span class="token operator">=</span> r<span class="token string">"(\w+)\.(\w+)\.(?P<domain>\w+)"</span>
match <span class="token operator">=</span> re<span class="token punctuation">.</span>match<span class="token punctuation">(</span>pattern<span class="token punctuation">,</span> str1<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>match<span class="token punctuation">)</span> <span class="token comment">#None</span>
match <span class="token operator">=</span> re<span class="token punctuation">.</span>match<span class="token punctuation">(</span>pattern<span class="token punctuation">,</span> str2<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>match<span class="token punctuation">)</span> <span class="token comment">#<_sre.SRE_Match object; span=(0, 13), match='www.baidu.com'></span>
<span class="token keyword">if</span> match<span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>match<span class="token punctuation">.</span>pos<span class="token punctuation">)</span> <span class="token comment">#0</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>match<span class="token punctuation">.</span>endpos<span class="token punctuation">)</span> <span class="token comment">#41</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>match<span class="token punctuation">.</span>group<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">#www.baidu.com</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>match<span class="token punctuation">.</span>group<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">#baidu</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>match<span class="token punctuation">.</span>group<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">#('baidu', 'com')</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>match<span class="token punctuation">.</span>groups<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">#('www', 'baidu', 'com')</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>match<span class="token punctuation">.</span>groupdict<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">#{'domain': 'com'}</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>match<span class="token punctuation">.</span>span<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">#(0, 13)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>match<span class="token punctuation">.</span>span<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">#(10, 13)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>match<span class="token punctuation">.</span>start<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">#10</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>match<span class="token punctuation">.</span>expand<span class="token punctuation">(</span>r<span class="token string">"\3 \1 \2 \g<domain>"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">#com www baidu com</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"No match!!!"</span><span class="token punctuation">)</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div><h3 id="research-han-shu"><a class="markdownIt-Anchor" href="#research-han-shu"></a> re.search函数</h3><p>re.search扫描整个字符串并返回第一个成功的匹配。</p><p><strong>函数语法:</strong></p><div class="prism-hl"><div class="code-tool"><div class="code-show" type="hide"><div class="show"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647 802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329L734.306083 294.306329 734.306083 294.306329C716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214L289.779333 293.88214 289.779333 293.88214C272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L221.990112 290.329458 16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231Z" id="Combined-Shape"></path></g></svg></div><div class="hide"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329 716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214 272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231ZM649.492098 134.243566C674.403037 143.310407 687.247217 170.85484 678.180377 195.765779L436.030115 861.068155C426.963275 885.979094 399.418842 898.823274 374.507902 889.756434 349.596963 880.689593 336.752783 853.14516 345.819623 828.234221L587.969885 162.931845 587.969885 162.931845C597.036725 138.020906 624.581158 125.176726 649.492098 134.243566Z" id="Combined-Shape"></path></g></svg></div></div><div class="copy" aria-label=""><svg preserveAspectRatio="xMidYMid meet" fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor" class="icon-7f6730be--text-3f89f380"><g><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></g></svg></div></div><pre data_role="codeBlock" class="language-python">re<span class="token punctuation">.</span>search<span class="token punctuation">(</span>pattern<span class="token punctuation">,</span> string<span class="token punctuation">[</span><span class="token punctuation">,</span> flags<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</pre></div><p><strong>实例</strong></p><div class="prism-hl"><div class="code-tool"><div class="code-show" type="hide"><div class="show"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647 802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329L734.306083 294.306329 734.306083 294.306329C716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214L289.779333 293.88214 289.779333 293.88214C272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L221.990112 290.329458 16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231Z" id="Combined-Shape"></path></g></svg></div><div class="hide"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329 716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214 272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231ZM649.492098 134.243566C674.403037 143.310407 687.247217 170.85484 678.180377 195.765779L436.030115 861.068155C426.963275 885.979094 399.418842 898.823274 374.507902 889.756434 349.596963 880.689593 336.752783 853.14516 345.819623 828.234221L587.969885 162.931845 587.969885 162.931845C597.036725 138.020906 624.581158 125.176726 649.492098 134.243566Z" id="Combined-Shape"></path></g></svg></div></div><div class="copy" aria-label=""><svg preserveAspectRatio="xMidYMid meet" fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor" class="icon-7f6730be--text-3f89f380"><g><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></g></svg></div></div><pre data_role="codeBlock" class="language-python line-numbers"><span class="token comment">#!/usr/bin/env python</span>
<span class="token keyword">import</span> re
str1 <span class="token operator">=</span> <span class="token triple-quoted-string string">"""baidu.com
www.google.com
www.12306.cn"""</span>
str2 <span class="token operator">=</span> <span class="token triple-quoted-string string">"""www.baidu.com
www.google.com
www.12306.cn"""</span>
pattern <span class="token operator">=</span> r<span class="token string">"(\w+)\.(\w+)\.(?P<domain>\w+)"</span>
match <span class="token operator">=</span> re<span class="token punctuation">.</span>search<span class="token punctuation">(</span>pattern<span class="token punctuation">,</span> str1<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>match<span class="token punctuation">)</span> <span class="token comment">#<_sre.SRE_Match object; span=(10, 24), match='www.google.com'></span>
match <span class="token operator">=</span> re<span class="token punctuation">.</span>search<span class="token punctuation">(</span>pattern<span class="token punctuation">,</span> str2<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>match<span class="token punctuation">)</span> <span class="token comment">#<_sre.SRE_Match object; span=(0, 13), match='www.baidu.com'></span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div><h3 id="refindall-han-shu"><a class="markdownIt-Anchor" href="#refindall-han-shu"></a> re.findall函数</h3><p>在字符串中找到正则表达式所匹配的所有子串,并返回一个列表,如果没有找到匹配的,则返回空列表。</p><p><strong>函数语法:</strong></p><div class="prism-hl"><div class="code-tool"><div class="code-show" type="hide"><div class="show"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647 802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329L734.306083 294.306329 734.306083 294.306329C716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214L289.779333 293.88214 289.779333 293.88214C272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L221.990112 290.329458 16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231Z" id="Combined-Shape"></path></g></svg></div><div class="hide"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329 716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214 272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231ZM649.492098 134.243566C674.403037 143.310407 687.247217 170.85484 678.180377 195.765779L436.030115 861.068155C426.963275 885.979094 399.418842 898.823274 374.507902 889.756434 349.596963 880.689593 336.752783 853.14516 345.819623 828.234221L587.969885 162.931845 587.969885 162.931845C597.036725 138.020906 624.581158 125.176726 649.492098 134.243566Z" id="Combined-Shape"></path></g></svg></div></div><div class="copy" aria-label=""><svg preserveAspectRatio="xMidYMid meet" fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor" class="icon-7f6730be--text-3f89f380"><g><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></g></svg></div></div><pre data_role="codeBlock" class="language-python">re<span class="token punctuation">.</span>findall<span class="token punctuation">(</span>pattern<span class="token punctuation">,</span> string<span class="token punctuation">[</span><span class="token punctuation">,</span> flags<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</pre></div><p><strong>实例</strong></p><div class="prism-hl"><div class="code-tool"><div class="code-show" type="hide"><div class="show"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647 802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329L734.306083 294.306329 734.306083 294.306329C716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214L289.779333 293.88214 289.779333 293.88214C272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L221.990112 290.329458 16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231Z" id="Combined-Shape"></path></g></svg></div><div class="hide"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329 716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214 272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231ZM649.492098 134.243566C674.403037 143.310407 687.247217 170.85484 678.180377 195.765779L436.030115 861.068155C426.963275 885.979094 399.418842 898.823274 374.507902 889.756434 349.596963 880.689593 336.752783 853.14516 345.819623 828.234221L587.969885 162.931845 587.969885 162.931845C597.036725 138.020906 624.581158 125.176726 649.492098 134.243566Z" id="Combined-Shape"></path></g></svg></div></div><div class="copy" aria-label=""><svg preserveAspectRatio="xMidYMid meet" fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor" class="icon-7f6730be--text-3f89f380"><g><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></g></svg></div></div><pre data_role="codeBlock" class="language-python"><span class="token comment">#!/usr/bin/env python</span>
<span class="token keyword">import</span> re
text <span class="token operator">=</span> <span class="token triple-quoted-string string">"""www.baidu.com
Site: www.google.com
www.12306.cn"""</span>
pattern <span class="token operator">=</span> r<span class="token string">"(\w+)\.(\w+)\.(\w+)"</span>
match <span class="token operator">=</span> re<span class="token punctuation">.</span>findall<span class="token punctuation">(</span>pattern<span class="token punctuation">,</span> text<span class="token punctuation">,</span> re<span class="token punctuation">.</span>M<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>match<span class="token punctuation">)</span> <span class="token comment">#[('www', 'baidu', 'com'), ('www', 'google', 'com'), ('www', '12306', 'cn')]</span>
</pre></div><h3 id="refinditer-han-shu"><a class="markdownIt-Anchor" href="#refinditer-han-shu"></a> re.finditer函数</h3><p>搜索string,返回一个顺序访问每一个匹配结果(Match对象)的迭代器<br><strong>函数语法:</strong></p><div class="prism-hl"><div class="code-tool"><div class="code-show" type="hide"><div class="show"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647 802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329L734.306083 294.306329 734.306083 294.306329C716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214L289.779333 293.88214 289.779333 293.88214C272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L221.990112 290.329458 16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231Z" id="Combined-Shape"></path></g></svg></div><div class="hide"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329 716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214 272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231ZM649.492098 134.243566C674.403037 143.310407 687.247217 170.85484 678.180377 195.765779L436.030115 861.068155C426.963275 885.979094 399.418842 898.823274 374.507902 889.756434 349.596963 880.689593 336.752783 853.14516 345.819623 828.234221L587.969885 162.931845 587.969885 162.931845C597.036725 138.020906 624.581158 125.176726 649.492098 134.243566Z" id="Combined-Shape"></path></g></svg></div></div><div class="copy" aria-label=""><svg preserveAspectRatio="xMidYMid meet" fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor" class="icon-7f6730be--text-3f89f380"><g><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></g></svg></div></div><pre data_role="codeBlock" class="language-python">re<span class="token punctuation">.</span>finditer<span class="token punctuation">(</span>pattern<span class="token punctuation">,</span> string<span class="token punctuation">[</span><span class="token punctuation">,</span> flags<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</pre></div><p><strong>实例</strong></p><div class="prism-hl"><div class="code-tool"><div class="code-show" type="hide"><div class="show"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647 802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329L734.306083 294.306329 734.306083 294.306329C716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214L289.779333 293.88214 289.779333 293.88214C272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L221.990112 290.329458 16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231Z" id="Combined-Shape"></path></g></svg></div><div class="hide"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329 716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214 272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231ZM649.492098 134.243566C674.403037 143.310407 687.247217 170.85484 678.180377 195.765779L436.030115 861.068155C426.963275 885.979094 399.418842 898.823274 374.507902 889.756434 349.596963 880.689593 336.752783 853.14516 345.819623 828.234221L587.969885 162.931845 587.969885 162.931845C597.036725 138.020906 624.581158 125.176726 649.492098 134.243566Z" id="Combined-Shape"></path></g></svg></div></div><div class="copy" aria-label=""><svg preserveAspectRatio="xMidYMid meet" fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor" class="icon-7f6730be--text-3f89f380"><g><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></g></svg></div></div><pre data_role="codeBlock" class="language-python"><span class="token comment">#!/usr/bin/env python</span>
<span class="token keyword">import</span> re
<span class="token keyword">for</span> m <span class="token keyword">in</span> re<span class="token punctuation">.</span>finditer<span class="token punctuation">(</span>r<span class="token string">"\d+"</span><span class="token punctuation">,</span><span class="token string">'one1two2three3four4'</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>m<span class="token punctuation">.</span>group<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> end<span class="token operator">=</span><span class="token string">" "</span><span class="token punctuation">)</span> <span class="token comment">#1 2 3 4</span>
</pre></div><h3 id="resub-han-shu"><a class="markdownIt-Anchor" href="#resub-han-shu"></a> re.sub函数</h3><p>使用repl替换string中每一个匹配的子串后返回替换后的字符串。<br>当repl是一个字符串时,可以使用\id或\g<id>、\g<name>引用分组,但不能使用编号0。<br>当repl是一个函数时,<strong>这个函数应当只接受一个参数(Match对象)</strong>,并返回一个字符串用于替换(返回的字符串中不能再引用分组)。<br>count用于指定最多替换次数,不指定时全部替换。</name></id></p><p><strong>函数语法:</strong></p><div class="prism-hl"><div class="code-tool"><div class="code-show" type="hide"><div class="show"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647 802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329L734.306083 294.306329 734.306083 294.306329C716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214L289.779333 293.88214 289.779333 293.88214C272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L221.990112 290.329458 16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231Z" id="Combined-Shape"></path></g></svg></div><div class="hide"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329 716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214 272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231ZM649.492098 134.243566C674.403037 143.310407 687.247217 170.85484 678.180377 195.765779L436.030115 861.068155C426.963275 885.979094 399.418842 898.823274 374.507902 889.756434 349.596963 880.689593 336.752783 853.14516 345.819623 828.234221L587.969885 162.931845 587.969885 162.931845C597.036725 138.020906 624.581158 125.176726 649.492098 134.243566Z" id="Combined-Shape"></path></g></svg></div></div><div class="copy" aria-label=""><svg preserveAspectRatio="xMidYMid meet" fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor" class="icon-7f6730be--text-3f89f380"><g><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></g></svg></div></div><pre data_role="codeBlock" class="language-python">re<span class="token punctuation">.</span>sub<span class="token punctuation">(</span>pattern<span class="token punctuation">,</span> repl<span class="token punctuation">,</span> string<span class="token punctuation">[</span><span class="token punctuation">,</span> count<span class="token punctuation">]</span><span class="token punctuation">)</span>
</pre></div><p><strong>实例</strong></p><div class="prism-hl"><div class="code-tool"><div class="code-show" type="hide"><div class="show"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647 802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329L734.306083 294.306329 734.306083 294.306329C716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214L289.779333 293.88214 289.779333 293.88214C272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L221.990112 290.329458 16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231Z" id="Combined-Shape"></path></g></svg></div><div class="hide"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329 716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214 272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231ZM649.492098 134.243566C674.403037 143.310407 687.247217 170.85484 678.180377 195.765779L436.030115 861.068155C426.963275 885.979094 399.418842 898.823274 374.507902 889.756434 349.596963 880.689593 336.752783 853.14516 345.819623 828.234221L587.969885 162.931845 587.969885 162.931845C597.036725 138.020906 624.581158 125.176726 649.492098 134.243566Z" id="Combined-Shape"></path></g></svg></div></div><div class="copy" aria-label=""><svg preserveAspectRatio="xMidYMid meet" fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor" class="icon-7f6730be--text-3f89f380"><g><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></g></svg></div></div><pre data_role="codeBlock" class="language-python"><span class="token comment">#!/usr/bin/env python</span>
<span class="token keyword">import</span> re
text <span class="token operator">=</span> <span class="token string">'i say, hello world!'</span>
pattern <span class="token operator">=</span> r<span class="token string">'(\w+) (\w+)'</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>re<span class="token punctuation">.</span>sub<span class="token punctuation">(</span>pattern<span class="token punctuation">,</span> r<span class="token string">'\2 \1'</span><span class="token punctuation">,</span> text<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">#say i, world hello!</span>
<span class="token keyword">def</span> <span class="token function">func</span><span class="token punctuation">(</span>m<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">return</span> m<span class="token punctuation">.</span>group<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">' '</span> <span class="token operator">+</span> m<span class="token punctuation">.</span>group<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>re<span class="token punctuation">.</span>sub<span class="token punctuation">(</span>pattern<span class="token punctuation">,</span> func<span class="token punctuation">,</span> text<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">#I Say, Hello World!</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>re<span class="token punctuation">.</span>sub<span class="token punctuation">(</span>pattern<span class="token punctuation">,</span> func<span class="token punctuation">,</span> text<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">#I Say, hello world!</span>
</pre></div><h3 id="resubn-han-shu"><a class="markdownIt-Anchor" href="#resubn-han-shu"></a> re.subn函数</h3><p>返回 (sub(repl, string[, count]), 替换次数)。</p><p><strong>函数语法:</strong></p><div class="prism-hl"><div class="code-tool"><div class="code-show" type="hide"><div class="show"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647 802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329L734.306083 294.306329 734.306083 294.306329C716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214L289.779333 293.88214 289.779333 293.88214C272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L221.990112 290.329458 16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231Z" id="Combined-Shape"></path></g></svg></div><div class="hide"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329 716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214 272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231ZM649.492098 134.243566C674.403037 143.310407 687.247217 170.85484 678.180377 195.765779L436.030115 861.068155C426.963275 885.979094 399.418842 898.823274 374.507902 889.756434 349.596963 880.689593 336.752783 853.14516 345.819623 828.234221L587.969885 162.931845 587.969885 162.931845C597.036725 138.020906 624.581158 125.176726 649.492098 134.243566Z" id="Combined-Shape"></path></g></svg></div></div><div class="copy" aria-label=""><svg preserveAspectRatio="xMidYMid meet" fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor" class="icon-7f6730be--text-3f89f380"><g><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></g></svg></div></div><pre data_role="codeBlock" class="language-python">re<span class="token punctuation">.</span>subn<span class="token punctuation">(</span>pattern<span class="token punctuation">,</span> repl<span class="token punctuation">,</span> string<span class="token punctuation">[</span><span class="token punctuation">,</span> count<span class="token punctuation">]</span><span class="token punctuation">)</span>
</pre></div><p><strong>实例</strong></p><div class="prism-hl"><div class="code-tool"><div class="code-show" type="hide"><div class="show"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647 802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329L734.306083 294.306329 734.306083 294.306329C716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214L289.779333 293.88214 289.779333 293.88214C272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L221.990112 290.329458 16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231Z" id="Combined-Shape"></path></g></svg></div><div class="hide"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329 716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214 272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231ZM649.492098 134.243566C674.403037 143.310407 687.247217 170.85484 678.180377 195.765779L436.030115 861.068155C426.963275 885.979094 399.418842 898.823274 374.507902 889.756434 349.596963 880.689593 336.752783 853.14516 345.819623 828.234221L587.969885 162.931845 587.969885 162.931845C597.036725 138.020906 624.581158 125.176726 649.492098 134.243566Z" id="Combined-Shape"></path></g></svg></div></div><div class="copy" aria-label=""><svg preserveAspectRatio="xMidYMid meet" fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor" class="icon-7f6730be--text-3f89f380"><g><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></g></svg></div></div><pre data_role="codeBlock" class="language-python"><span class="token comment">#!/usr/bin/env python</span>
<span class="token keyword">import</span> re
text <span class="token operator">=</span> <span class="token string">'i say, hello world!'</span>
pattern <span class="token operator">=</span> r<span class="token string">'(\w+) (\w+)'</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>re<span class="token punctuation">.</span>sub<span class="token punctuation">(</span>pattern<span class="token punctuation">,</span> r<span class="token string">'\2 \1'</span><span class="token punctuation">,</span> text<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">#('say i, world hello!', 2)</span>
<span class="token keyword">def</span> <span class="token function">func</span><span class="token punctuation">(</span>m<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">return</span> m<span class="token punctuation">.</span>group<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">' '</span> <span class="token operator">+</span> m<span class="token punctuation">.</span>group<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>re<span class="token punctuation">.</span>sub<span class="token punctuation">(</span>pattern<span class="token punctuation">,</span> func<span class="token punctuation">,</span> text<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">#('I Say, Hello World!', 2)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>re<span class="token punctuation">.</span>sub<span class="token punctuation">(</span>pattern<span class="token punctuation">,</span> func<span class="token punctuation">,</span> text<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">#('I Say, hello world!', 1)</span>
</pre></div><h3 id="resplit-han-shu"><a class="markdownIt-Anchor" href="#resplit-han-shu"></a> re.split函数</h3><p>按照能够匹配的子串将string分割后返回列表。maxsplit用于指定最大分割次数,不指定将全部分割。</p><p><strong>函数语法:</strong></p><div class="prism-hl"><div class="code-tool"><div class="code-show" type="hide"><div class="show"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647 802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329L734.306083 294.306329 734.306083 294.306329C716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214L289.779333 293.88214 289.779333 293.88214C272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L221.990112 290.329458 16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231Z" id="Combined-Shape"></path></g></svg></div><div class="hide"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329 716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214 272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231ZM649.492098 134.243566C674.403037 143.310407 687.247217 170.85484 678.180377 195.765779L436.030115 861.068155C426.963275 885.979094 399.418842 898.823274 374.507902 889.756434 349.596963 880.689593 336.752783 853.14516 345.819623 828.234221L587.969885 162.931845 587.969885 162.931845C597.036725 138.020906 624.581158 125.176726 649.492098 134.243566Z" id="Combined-Shape"></path></g></svg></div></div><div class="copy" aria-label=""><svg preserveAspectRatio="xMidYMid meet" fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor" class="icon-7f6730be--text-3f89f380"><g><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></g></svg></div></div><pre data_role="codeBlock" class="language-python">re<span class="token punctuation">.</span>split<span class="token punctuation">(</span>pattern<span class="token punctuation">,</span> string<span class="token punctuation">[</span><span class="token punctuation">,</span> maxsplit<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
</pre></div><p><strong>实例</strong></p><div class="prism-hl"><div class="code-tool"><div class="code-show" type="hide"><div class="show"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647 802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329L734.306083 294.306329 734.306083 294.306329C716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214L289.779333 293.88214 289.779333 293.88214C272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L221.990112 290.329458 16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231Z" id="Combined-Shape"></path></g></svg></div><div class="hide"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329 716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214 272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231ZM649.492098 134.243566C674.403037 143.310407 687.247217 170.85484 678.180377 195.765779L436.030115 861.068155C426.963275 885.979094 399.418842 898.823274 374.507902 889.756434 349.596963 880.689593 336.752783 853.14516 345.819623 828.234221L587.969885 162.931845 587.969885 162.931845C597.036725 138.020906 624.581158 125.176726 649.492098 134.243566Z" id="Combined-Shape"></path></g></svg></div></div><div class="copy" aria-label=""><svg preserveAspectRatio="xMidYMid meet" fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor" class="icon-7f6730be--text-3f89f380"><g><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></g></svg></div></div><pre data_role="codeBlock" class="language-python"><span class="token comment">#!/usr/bin/env python</span>
<span class="token keyword">import</span> re
text <span class="token operator">=</span> <span class="token string">'one1two2three3four4'</span>
pattern <span class="token operator">=</span> r<span class="token string">"\d+"</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>re<span class="token punctuation">.</span>split<span class="token punctuation">(</span>pattern<span class="token punctuation">,</span> text<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">#['one', 'two', 'three', 'four', '']</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>re<span class="token punctuation">.</span>split<span class="token punctuation">(</span>pattern<span class="token punctuation">,</span> text<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">#['one', 'two', 'three3four4']</span>
</pre></div><h2 id="pattern-dui-xiang"><a class="markdownIt-Anchor" href="#pattern-dui-xiang"></a> Pattern对象</h2><p><strong>re.compile函数构造:</strong></p><div class="prism-hl"><div class="code-tool"><div class="code-show" type="hide"><div class="show"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647 802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329L734.306083 294.306329 734.306083 294.306329C716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214L289.779333 293.88214 289.779333 293.88214C272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L221.990112 290.329458 16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231Z" id="Combined-Shape"></path></g></svg></div><div class="hide"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329 716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214 272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231ZM649.492098 134.243566C674.403037 143.310407 687.247217 170.85484 678.180377 195.765779L436.030115 861.068155C426.963275 885.979094 399.418842 898.823274 374.507902 889.756434 349.596963 880.689593 336.752783 853.14516 345.819623 828.234221L587.969885 162.931845 587.969885 162.931845C597.036725 138.020906 624.581158 125.176726 649.492098 134.243566Z" id="Combined-Shape"></path></g></svg></div></div><div class="copy" aria-label=""><svg preserveAspectRatio="xMidYMid meet" fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor" class="icon-7f6730be--text-3f89f380"><g><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></g></svg></div></div><pre data_role="codeBlock" class="language-python">re<span class="token punctuation">.</span><span class="token builtin">compile</span><span class="token punctuation">(</span>strPattern<span class="token punctuation">[</span><span class="token punctuation">,</span> flag<span class="token punctuation">]</span><span class="token punctuation">)</span>
</pre></div><p><strong>方法:</strong></p><ul data-mark="*"><li><code>match(string[, pos[, endpos]])</code>:功能等同于re.match函数。不同的是可以指定匹配的起始与终止下标,pos和endpos的默认值分别为0和len(string),re.match()无法指定这两个参数。</li><li><code>search(string[, pos[, endpos]])</code>:同上</li><li><code>findall(string[, pos[, endpos]])</code>:同上</li><li><code>finditer(string[, pos[, endpos]])</code>:同上</li><li><code>sub(repl, string[, count])</code></li><li><code>subn(repl, string[, count])</code></li><li><code>split(string[, maxsplit])</code></li></ul><p><strong>实例</strong></p><div class="prism-hl"><div class="code-tool"><div class="code-show" type="hide"><div class="show"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647 802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329L734.306083 294.306329 734.306083 294.306329C716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214L289.779333 293.88214 289.779333 293.88214C272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L221.990112 290.329458 16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231Z" id="Combined-Shape"></path></g></svg></div><div class="hide"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329 716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214 272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231ZM649.492098 134.243566C674.403037 143.310407 687.247217 170.85484 678.180377 195.765779L436.030115 861.068155C426.963275 885.979094 399.418842 898.823274 374.507902 889.756434 349.596963 880.689593 336.752783 853.14516 345.819623 828.234221L587.969885 162.931845 587.969885 162.931845C597.036725 138.020906 624.581158 125.176726 649.492098 134.243566Z" id="Combined-Shape"></path></g></svg></div></div><div class="copy" aria-label=""><svg preserveAspectRatio="xMidYMid meet" fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor" class="icon-7f6730be--text-3f89f380"><g><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></g></svg></div></div><pre data_role="codeBlock" class="language-python"><span class="token comment">#!/usr/bin/env python</span>
<span class="token keyword">import</span> re
s <span class="token operator">=</span> <span class="token string">"Steven is 17 years old."</span>
p <span class="token operator">=</span> re<span class="token punctuation">.</span><span class="token builtin">compile</span><span class="token punctuation">(</span>r<span class="token string">"\d+"</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>p<span class="token punctuation">.</span>match<span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">#None</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>p<span class="token punctuation">.</span>match<span class="token punctuation">(</span>s<span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">#<_sre.SRE_Match object; span=(10, 12), match='17'></span>
</pre></div><h2 id="pi-pei-fang-shi-flags"><a class="markdownIt-Anchor" href="#pi-pei-fang-shi-flags"></a> 匹配方式–flags</h2><h3 id="rem"><a class="markdownIt-Anchor" href="#rem"></a> re.M</h3><p>实例:替换多行的行首note为warning</p><div class="prism-hl"><div class="code-tool"><div class="code-show" type="hide"><div class="show"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647 802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329L734.306083 294.306329 734.306083 294.306329C716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214L289.779333 293.88214 289.779333 293.88214C272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L221.990112 290.329458 16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231Z" id="Combined-Shape"></path></g></svg></div><div class="hide"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329 716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214 272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231ZM649.492098 134.243566C674.403037 143.310407 687.247217 170.85484 678.180377 195.765779L436.030115 861.068155C426.963275 885.979094 399.418842 898.823274 374.507902 889.756434 349.596963 880.689593 336.752783 853.14516 345.819623 828.234221L587.969885 162.931845 587.969885 162.931845C597.036725 138.020906 624.581158 125.176726 649.492098 134.243566Z" id="Combined-Shape"></path></g></svg></div></div><div class="copy" aria-label=""><svg preserveAspectRatio="xMidYMid meet" fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor" class="icon-7f6730be--text-3f89f380"><g><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></g></svg></div></div><pre data_role="codeBlock" class="language-python"><span class="token comment">#!/usr/bin/env python</span>
<span class="token keyword">import</span> re
text <span class="token operator">=</span> <span class="token triple-quoted-string string">"""note: Meeting will begin at 8:00.
note: Please take your notebook.
note: Meeting will end at 10:00. (note: turn off the light)
"""</span>
p <span class="token operator">=</span> re<span class="token punctuation">.</span><span class="token builtin">compile</span><span class="token punctuation">(</span>r<span class="token string">"^note"</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token punctuation">(</span>p<span class="token punctuation">.</span>sub<span class="token punctuation">(</span><span class="token string">"warning"</span><span class="token punctuation">,</span>text<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">#only the first line has substitute</span>
p <span class="token operator">=</span> re<span class="token punctuation">.</span><span class="token builtin">compile</span><span class="token punctuation">(</span>r<span class="token string">"^note"</span><span class="token punctuation">,</span> re<span class="token punctuation">.</span>M<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token punctuation">(</span>p<span class="token punctuation">.</span>sub<span class="token punctuation">(</span><span class="token string">"warning"</span><span class="token punctuation">,</span>text<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">#sub all line</span>
</pre></div><h3 id="res"><a class="markdownIt-Anchor" href="#res"></a> re.S</h3><p>改变’.'符号的行为,使其能够匹配换行符</p><div class="prism-hl"><div class="code-tool"><div class="code-show" type="hide"><div class="show"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647 802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329L734.306083 294.306329 734.306083 294.306329C716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214L289.779333 293.88214 289.779333 293.88214C272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L221.990112 290.329458 16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231Z" id="Combined-Shape"></path></g></svg></div><div class="hide"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329 716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214 272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231ZM649.492098 134.243566C674.403037 143.310407 687.247217 170.85484 678.180377 195.765779L436.030115 861.068155C426.963275 885.979094 399.418842 898.823274 374.507902 889.756434 349.596963 880.689593 336.752783 853.14516 345.819623 828.234221L587.969885 162.931845 587.969885 162.931845C597.036725 138.020906 624.581158 125.176726 649.492098 134.243566Z" id="Combined-Shape"></path></g></svg></div></div><div class="copy" aria-label=""><svg preserveAspectRatio="xMidYMid meet" fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor" class="icon-7f6730be--text-3f89f380"><g><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></g></svg></div></div><pre data_role="codeBlock" class="language-python line-numbers"><span class="token comment">#!/usr/bin/env python</span>
<span class="token keyword">import</span> re
text <span class="token operator">=</span> <span class="token triple-quoted-string string">"""
parameter MODE = 1 ,
DIN_WIDTH = 32,
DOUT_WIDTH = 24;
"""</span>
p <span class="token operator">=</span> re<span class="token punctuation">.</span><span class="token builtin">compile</span><span class="token punctuation">(</span>r<span class="token string">"parameter(.*);"</span><span class="token punctuation">)</span>
m <span class="token operator">=</span> p<span class="token punctuation">.</span>search<span class="token punctuation">(</span>text<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>m<span class="token punctuation">)</span> <span class="token comment">#None</span>
p <span class="token operator">=</span> re<span class="token punctuation">.</span><span class="token builtin">compile</span><span class="token punctuation">(</span>r<span class="token string">"parameter\s+(.*);"</span><span class="token punctuation">,</span> re<span class="token punctuation">.</span>S<span class="token punctuation">)</span>
m <span class="token operator">=</span> p<span class="token punctuation">.</span>search<span class="token punctuation">(</span>text<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>m<span class="token punctuation">.</span>group<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div><h2 id="tan-lan-mo-shi"><a class="markdownIt-Anchor" href="#tan-lan-mo-shi"></a> 贪婪模式</h2><p>Python里数量词默认是贪婪的,总是尝试匹配尽可能多的字符;非贪婪的则相反,总是尝试匹配尽可能少的字符。例如:正则表达式"ab*“如果用于查找"abbbc”,将找到"abbb"。而如果使用非贪婪的数量词"ab*?",将找到"a"。</p><div class="prism-hl"><div class="code-tool"><div class="code-show" type="hide"><div class="show"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647 802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329L734.306083 294.306329 734.306083 294.306329C716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214L289.779333 293.88214 289.779333 293.88214C272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L221.990112 290.329458 16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231Z" id="Combined-Shape"></path></g></svg></div><div class="hide"><svg viewBox="0 0 1024 1024" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor"><g id="小站" fill="#9daab6" stroke-width="1" fill-rule="evenodd"><path d="M1018.64495 531.297637C1027.27952 512.687401 1023.24618 489.87879 1007.20328 475.433694L802.095304 290.753647C782.394782 273.015217 752.044514 274.605807 734.306083 294.306329 716.567653 314.006852 718.158243 344.35712 737.858766 362.09555L904.138417 511.81442 736.858766 662.433694C717.158243 680.172125 715.567653 710.522392 733.306083 730.222915 751.044514 749.923438 781.394782 751.514028 801.095304 733.775598L1006.20328 549.09555C1011.84552 544.015251 1016.00229 537.90046 1018.64495 531.297643ZM119.947 511.390231 286.22665 361.671361C305.927173 343.932931 307.517763 313.582663 289.779333 293.88214 272.040903 274.181618 241.690635 272.591027 221.990112 290.329458L16.8821402 475.009505C.839236202 489.454601-3.19410198 512.263212 5.44046645 530.873448 8.08312579 537.476271 12.2398959 543.591061 17.8821402 548.671361L222.990112 733.351408C242.690635 751.089839 273.040903 749.499248 290.779333 729.798726 308.517763 710.098203 306.927173 679.747935 287.22665 662.009505L119.947 511.390231ZM649.492098 134.243566C674.403037 143.310407 687.247217 170.85484 678.180377 195.765779L436.030115 861.068155C426.963275 885.979094 399.418842 898.823274 374.507902 889.756434 349.596963 880.689593 336.752783 853.14516 345.819623 828.234221L587.969885 162.931845 587.969885 162.931845C597.036725 138.020906 624.581158 125.176726 649.492098 134.243566Z" id="Combined-Shape"></path></g></svg></div></div><div class="copy" aria-label=""><svg preserveAspectRatio="xMidYMid meet" fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" stroke="currentColor" class="icon-7f6730be--text-3f89f380"><g><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></g></svg></div></div><pre data_role="codeBlock" class="language-python"><span class="token comment">#!/usr/bin/env python</span>
<span class="token keyword">import</span> re
text <span class="token operator">=</span> <span class="token string">"abcaxc"</span>
m <span class="token operator">=</span> re<span class="token punctuation">.</span>match<span class="token punctuation">(</span>r<span class="token string">"ab.*c"</span><span class="token punctuation">,</span>text<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>m<span class="token punctuation">.</span>group<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">#abcaxc</span>
m <span class="token operator">=</span> re<span class="token punctuation">.</span>match<span class="token punctuation">(</span>r<span class="token string">"ab.*?c"</span><span class="token punctuation">,</span>text<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>m<span class="token punctuation">.</span>group<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">#abc</span>
</pre></div><div id="post-donate"><div class="donate_msg">生活不止眼前的苟且,还有诗和远方</div><button><span>打赏</span></button><div class="QR" style="display:none"><div class="alipay_qr"><img src="https://fastly.jsdelivr.net/gh/dxsm/cdn/images/alipay.png" alt="淘宝支付"><p>淘宝支付</p></div><div class="weixin_qr"><img src="https://fastly.jsdelivr.net/gh/dxsm/cdn/images/weixinpay.png" alt="微信支付"><p>微信支付</p></div></div></div><blockquote class="post-copyright"><p class="post-copyright-author"><strong>本文作者:</strong> 董续胜</p><p class="post-copyright-link"><strong>本文链接:</strong> <a href="https://dxsm.github.io/p/python-regular-expression.html" title="Python正则表达式">https://dxsm.github.io/p/python-regular-expression.html</a></p><p class="post-copyright-license"><strong>版权声明:</strong> 本博客所有文章除特别声明外,均采用<a href="https://creativecommons.org/licenses/by-nc-sa/3.0/" rel="external nofollow" target="_blank">CC BY-NC-SA 3.0</a>许可协议。转载请注明出处!</p></blockquote></div><section class="meta" id="footer-meta"><hr><div class="new-meta-box"><div class="new-meta-item date" itemprop="dateUpdated" datetime="2022-04-18T12:11:40+08:00"><a class="notlink"><i class="fa fa-pencil-alt" aria-hidden="true"></i> 2022-04-18</a></div><div class="new-meta-item meta-tags"><a class="tag" href="/tags/Python/"><i class="fa fa-hashtag" aria-hidden="true"></i> Python</a></div><div class="new-meta-item meta-tags"><a class="tag" href="/tags/%E6%AD%A3%E5%88%99/"><i class="fa fa-hashtag" aria-hidden="true"></i> 正则</a></div></div></section><div class="prev-next"><section class="prev"><span class="art-item-left"><h6><i class="fa fa-chevron-left" aria-hidden="true"></i> 上一页</h6><h4><a href="/p/xin-pian-she-ji-xiao-shi-li-gong-xiang-buffer.html" rel="prev" title="芯片设计小实例之共享Buffer">芯片设计小实例之共享Buffer</a></h4><h6 class="tags"><a class="tag" href="/tags/%E8%AE%BE%E8%AE%A1%E6%8A%80%E5%B7%A7/"><i class="fa fa-hashtag fa-fw" aria-hidden="true"></i> 设计技巧</a><a class="tag" href="/tags/%E8%B5%84%E6%BA%90%E4%BC%98%E5%8C%96/"><i class="fa fa-hashtag fa-fw" aria-hidden="true"></i> 资源优化</a></h6></span></section><section class="next"><span class="art-item-right" aria-hidden="true"><h6>下一页 <i class="fa fa-chevron-right" aria-hidden="true"></i></h6><h4><a href="/p/shell-cfg.html" rel="prev" title="Shell配置方案">Shell配置方案</a></h4><h6 class="tags"><a class="tag" href="/tags/linux/"><i class="fa fa-hashtag fa-fw" aria-hidden="true"></i> linux</a></h6></span></section></div></section></article><br><article class="post white-box comments"><section class="article typo"><h4><i class="fa fa-comments fa-fw" aria-hidden="true"></i> 评论</h4><section id="comments"><div id="valine_container" class="valine_thread"><i class="fa fa-spinner fa-spin fa-fw"></i></div></section></section></article><script>window.subData={title:"Python正则表达式",tools:!0}</script></div><aside class="l_side"><section class="author"><div class="content pure"><div class="avatar"><img class="avatar" src="https://fastly.jsdelivr.net/gh/dxsm/cdn/images/loading.svg" data-echo="https://fastly.jsdelivr.net/gh/dxsm/cdn/images/avatar3.jpg"></div><div class="text"><p class="nickname">星雨夜澈</p><p><span class="descr">不忘初心,记录时光,走向成长</span></p></div><div class="social-wrapper"><a href="http://www.uestc.edu.cn/" title="2016年毕业于电子科技大学" class="social flat-btn" target="_blank" rel="external nofollow"><i class="social fa fa-university fa_fw" aria-hidden="true"></i></a><a href="http://gr.uestc.edu.cn/" title="硕士学历" class="social flat-btn" target="_blank" rel="external nofollow"><i class="social fa fa-graduation-cap fa_fw" aria-hidden="true"></i></a><a href="#" title="中国 上海" class="social flat-btn" target="_blank" rel="external nofollow"><i class="social fa fa-map-marker fa_fw" aria-hidden="true"></i></a><a href="#" title="QQ: 959229640" class="social flat-btn" target="_blank" rel="external nofollow"><i class="social fa fa-qq fa_fw" aria-hidden="true"></i></a><a href="mailto:[email protected]" title="[email protected]" class="social flat-btn" target="_blank" rel="external nofollow"><i class="social fa fa-envelope fa_fw" aria-hidden="true"></i></a></div></div></section><section class="plain"><header class="pure"><div><i class="fa fa-bullhorn font-animation annouce fa-fw" aria-hidden="true"></i> 公告</div></header><div class="content pure"><p>本站基本建设完成,喜欢的话可以安利给身边的朋友哦!最近已添加markdown在线工具</p></div></section><section class="toc-wrapper"><header class="pure"><div><i class="fa fa-list-ul fa-fw" aria-hidden="true"></i> 文章目录</div><div class="wrapper"><a class="s-toc rightBtn" rel="external nofollow noopener noreferrer" href="javascript:void(0)"><i class="fa fa-thumbtack fa-fw"></i></a></div></header><div class="content pure"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#wei-shi-me-shi-yong-zheng-ze-biao-da-shi"><span class="toc-text">为什么使用正则表达式?</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#pi-pei-gui-ze"><span class="toc-text">匹配规则</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#re-mo-kuai-chang-yong-han-shu"><span class="toc-text">re模块常用函数</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#rematch-han-shu"><span class="toc-text">re.match函数</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#fan-hui-zhi-match-dui-xiang"><span class="toc-text">返回值match对象</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#shi-li"><span class="toc-text">实例</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#research-han-shu"><span class="toc-text">re.search函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#refindall-han-shu"><span class="toc-text">re.findall函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#refinditer-han-shu"><span class="toc-text">re.finditer函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#resub-han-shu"><span class="toc-text">re.sub函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#resubn-han-shu"><span class="toc-text">re.subn函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#resplit-han-shu"><span class="toc-text">re.split函数</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#pattern-dui-xiang"><span class="toc-text">Pattern对象</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#pi-pei-fang-shi-flags"><span class="toc-text">匹配方式–flags</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#rem"><span class="toc-text">re.M</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#res"><span class="toc-text">re.S</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#tan-lan-mo-shi"><span class="toc-text">贪婪模式</span></a></li></ol></div></section><section class="tagcloud"><header class="pure"><div><i class="fa fa-hashtag fa-fw" aria-hidden="true"></i> 热门标签</div><a class="rightBtn" rel="external nofollow noopener noreferrer" href="/tags/" title="tags/"><i class="fa fa-arrows-alt fa-fw"></i></a></header><div class="content pure"><a href="/tags/AHB/" style="font-size:14px;color:#999">AHB</a> <a href="/tags/AMBA/" style="font-size:16px;color:#8b8b8b">AMBA</a> <a href="/tags/ASIC/" style="font-size:24px;color:#555">ASIC</a> <a href="/tags/AXI/" style="font-size:14px;color:#999">AXI</a> <a href="/tags/Low-Power/" style="font-size:18px;color:#7e7e7e">Low Power</a> <a href="/tags/Python/" style="font-size:22px;color:#636363">Python</a> <a href="/tags/SOC/" style="font-size:18px;color:#7e7e7e">SOC</a> <a href="/tags/SpinalHDL/" style="font-size:24px;color:#555">SpinalHDL</a> <a href="/tags/UPF/" style="font-size:14px;color:#999">UPF</a> <a href="/tags/eda-tools/" style="font-size:16px;color:#8b8b8b">eda_tools</a> <a href="/tags/electron/" style="font-size:14px;color:#999">electron</a> <a href="/tags/electron-vue/" style="font-size:14px;color:#999">electron-vue</a> <a href="/tags/fifo/" style="font-size:14px;color:#999">fifo</a> <a href="/tags/git/" style="font-size:14px;color:#999">git</a> <a href="/tags/jtag/" style="font-size:14px;color:#999">jtag</a> <a href="/tags/linux/" style="font-size:22px;color:#636363">linux</a> <a href="/tags/markdown/" style="font-size:16px;color:#8b8b8b">markdown</a> <a href="/tags/systemverilog/" style="font-size:20px;color:#707070">systemverilog</a> <a href="/tags/testbench/" style="font-size:14px;color:#999">testbench</a> <a href="/tags/typora/" style="font-size:16px;color:#8b8b8b">typora</a> <a href="/tags/vim/" style="font-size:14px;color:#999">vim</a> <a href="/tags/vimrc/" style="font-size:14px;color:#999">vimrc</a> <a href="/tags/%E4%B9%A6%E7%B1%8D/" style="font-size:14px;color:#999">书籍</a> <a href="/tags/%E5%84%BF%E7%AB%A5%E6%95%99%E8%82%B2/" style="font-size:14px;color:#999">儿童教育</a> <a href="/tags/%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86/" style="font-size:14px;color:#999">内存管理</a> <a href="/tags/%E6%AD%A3%E5%88%99/" style="font-size:14px;color:#999">正则</a> <a href="/tags/%E6%B5%81%E4%BF%9D%E5%BA%8F/" style="font-size:14px;color:#999">流保序</a> <a href="/tags/%E8%AE%BE%E8%AE%A1%E6%8A%80%E5%B7%A7/" style="font-size:18px;color:#7e7e7e">设计技巧</a> <a href="/tags/%E8%B5%84%E6%BA%90%E4%BC%98%E5%8C%96/" style="font-size:16px;color:#8b8b8b">资源优化</a> <a href="/tags/%E9%9D%A2%E8%AF%95/" style="font-size:14px;color:#999">面试</a></div></section><section class="category"><header class="pure"><div><i class="fa fa-folder-open fa-fw" aria-hidden="true"></i> 所有分类</div><a class="rightBtn" rel="external nofollow noopener noreferrer" href="/categories/" title="categories/"><i class="fa fa-arrows-alt fa-fw"></i></a></header><div class="content pure"><ul class="entry"><li><a class="flat-box" title="/categories/ASIC%E6%B5%8B%E8%AF%95/" href="/categories/ASIC%E6%B5%8B%E8%AF%95/"><div class="name">ASIC测试</div><div class="badge">(1)</div></a></li><li><a class="flat-box" title="/categories/ASIC%E8%AE%BE%E8%AE%A1/" href="/categories/ASIC%E8%AE%BE%E8%AE%A1/"><div class="name">ASIC设计</div><div class="badge">(6)</div></a></li><li><a class="flat-box" title="/categories/ASIC%E9%AA%8C%E8%AF%81/" href="/categories/ASIC%E9%AA%8C%E8%AF%81/"><div class="name">ASIC验证</div><div class="badge">(5)</div></a></li><li><a class="flat-box" title="/categories/Python/" href="/categories/Python/"><div class="name">Python</div><div class="badge">(5)</div></a></li><li><a class="flat-box" title="/categories/SOC%E8%AE%BE%E8%AE%A1/" href="/categories/SOC%E8%AE%BE%E8%AE%A1/"><div class="name">SOC设计</div><div class="badge">(2)</div></a></li><li><a class="flat-box" title="/categories/SpinalHDL/" href="/categories/SpinalHDL/"><div class="name">SpinalHDL</div><div class="badge">(9)</div></a></li><li><a class="flat-box" title="/categories/UPF/" href="/categories/UPF/"><div class="name">UPF</div><div class="badge">(1)</div></a></li><li><a class="flat-box" title="/categories/eda-tools/" href="/categories/eda-tools/"><div class="name">eda_tools</div><div class="badge">(2)</div></a></li><li><a class="flat-box" title="/categories/electron/" href="/categories/electron/"><div class="name">electron</div><div class="badge">(1)</div></a></li><li><a class="flat-box" title="/categories/git/" href="/categories/git/"><div class="name">git</div><div class="badge">(1)</div></a></li><li><a class="flat-box" title="/categories/linux/" href="/categories/linux/"><div class="name">linux</div><div class="badge">(5)</div></a></li><li><a class="flat-box" title="/categories/markdown/" href="/categories/markdown/"><div class="name">markdown</div><div class="badge">(2)</div></a></li><li><a class="flat-box" title="/categories/typora/" href="/categories/typora/"><div class="name">typora</div><div class="badge">(2)</div></a></li><li><a class="flat-box" title="/categories/vim/" href="/categories/vim/"><div class="name">vim</div><div class="badge">(1)</div></a></li><li><a class="flat-box" title="/categories/%E4%BD%8E%E5%8A%9F%E8%80%97%E8%AE%BE%E8%AE%A1/" href="/categories/%E4%BD%8E%E5%8A%9F%E8%80%97%E8%AE%BE%E8%AE%A1/"><div class="name">低功耗设计</div><div class="badge">(2)</div></a></li><li><a class="flat-box" title="/categories/%E6%95%99%E8%82%B2/" href="/categories/%E6%95%99%E8%82%B2/"><div class="name">教育</div><div class="badge">(1)</div></a></li></ul></div></section></aside><script>setLoadingBarProgress(60)</script></div><a class="s-top fa fa-arrow-up fa-fw" href="javascript:void(0)"></a></div><footer id="footer" class="clearfix"><br><div><p>© 2019 <i class="fa fa-paper-plane font-animation"></i> 星雨夜澈</p></div></footer><script>setLoadingBarProgress(80)</script><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script><script>var GOOGLE_CUSTOM_SEARCH_API_KEY="",GOOGLE_CUSTOM_SEARCH_ENGINE_ID="",ALGOLIA_API_KEY="",ALGOLIA_APP_ID="",ALGOLIA_INDEX_NAME="",AZURE_SERVICE_NAME="",AZURE_INDEX_NAME="",AZURE_QUERY_KEY="",BAIDU_API_ID="",SEARCH_SERVICE="hexo",ROOT="/";"/"!=ROOT.substring(ROOT.length-1)&&(ROOT+="/")</script><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/dxsm/cdn/lib/fancybox/jquery.fancybox.min.css"><script src="https://cdn.jsdelivr.net/gh/dxsm/cdn/lib/fancybox/jquery.fancybox.min.js"></script><script>$(function(){$("body").find(".post-wrapper,#post").find("img").each(function(){var t=document.createElement("a");$(t).attr("data-fancybox","gallery"),$(t).attr("href",$(this).attr("src")),$(this).attr("data-echo")&&$(t).attr("href",$(this).attr("data-echo")),$(this).wrap(t)})})</script><script src="https://fastly.jsdelivr.net/npm/[email protected]/dist/Valine.min.js"></script><script>var GUEST_INFO=["nick","mail","link"],meta="nick,mail,link".split(",").filter(function(i){return-1<GUEST_INFO.indexOf(i)}),REQUIRED_FIELDS=["nick","mail","link"],requiredFields="nick,mail".split(",").filter(function(i){return-1<REQUIRED_FIELDS.indexOf(i)}),valine=new Valine;function emoji(i,e,a){return i+"/"+i+"-"+e+"."+a}for(var emojiMaps={},i=1;i<=54;i++)emojiMaps["tieba-"+i]=emoji("tieba",i,"png");for(i=1;i<=101;i++)emojiMaps["qq-"+i]=emoji("qq",i,"gif");for(i=1;i<=116;i++)emojiMaps["aru-"+i]=emoji("aru",i,"gif");for(i=1;i<=125;i++)emojiMaps["twemoji-"+i]=emoji("twemoji",i,"png");for(i=1;i<=4;i++)emojiMaps["weibo-"+i]=emoji("weibo",i,"png");valine.init({el:"#valine_container",meta:meta,appId:"luTuCxxHAQDleYzjxn1tNY3F-gzGzoHsz",appKey:"vWAPOyrP9jT4t1yQle11Qhzk",placeholder:"快来评论吧~",pageSize:"10",avatar:"robohash",lang:"zh-cn",visitor:"true",highlight:"true",mathJax:"false",enableQQ:"true",requiredFields:requiredFields,emojiCDN:"https://cdn.jsdelivr.net/gh/xaoxuu/cdn-assets/emoji/valine/",emojiMaps:emojiMaps})</script><script src="https://fastly.jsdelivr.net/gh/dxsm/[email protected]/js/main.js"></script><script src="https://cdn.jsdelivr.net/gh/toddmotto/[email protected]/src/echo.js"></script><script>echo.init({offset:100,throttle:250,unload:!1,callback:function(t,o){}})</script><script>window.onload=function(){if(/debug=true/.test(window.location)||"true"==localStorage.getItem("active-eruda")){var e=document.createElement("script");e.src="//cdn.jsdelivr.net/npm/eruda",e.async=!0,document.body.appendChild(e),e.onload=function(){eruda.init()}}}</script><script>setLoadingBarProgress(100)</script></body></html>