encoding: utf-8
import re
1. 匹配某个字符串:
text = "hello"
ret = re.match('he',text)
print(ret.group())
2. 点:匹配任意的字符
text = "\n"
ret = re.match('.',text)
print(ret.group())
3. \d:匹配任意的数字(0-9)
text = "+"
ret = re.match('\d',text)
print(ret.group())
4. \D:匹配任意的非数字
text = "2"
ret = re.match('\D',text)
print(ret.group())
5. \s:匹配空白字符(\n,\t,\r,空格)
text = "\r"
ret = re.match('\s',text)
print(ret.group())
6. \w:匹配的是a-z,A-Z,数字和下划线
text = "+"
ret = re.match('\w',text)
print(ret.group())
7. \W:与\w相反。
text = "a"
ret = re.match('\W',text)
print(ret.group())
8. []组合的方式,只要满足中括号中的字符,就可以匹配
text = "0731-88888888asfa"
ret = re.match('[\d-]+',text)
print(ret.group())
8.1. 中括号的形式代替\d
text = "09"
ret = re.match('[0-9]',text)
print(ret.group())
8.2. 中括号的形式代替\D
text = "1"
ret = re.match('[^0-9]',text)
print(ret.group())
8.3. 中括号的形式代替\w
text = "_"
ret = re.match('[a-zA-Z0-9_]',text)
print(ret.group())
8.4. 中括号的形式代替\W
text = "0"
ret = re.match('[^a-zA-Z0-9_]',text)
print(ret.group())
############ 匹配多个字符 ############
9. *:可以匹配0或者任意多个字符
text = "abcd"
ret = re.match('\s*',text)
print(ret.group())
10. +:匹配1个或者多个字符
text = "+abcd"
ret = re.match('\w+',text)
print(ret.group())
11. ?:匹配一个或者0个(要么没有,要么就只有一个)
text = "abcd"
ret = re.match('\w?',text)
print(ret.group())
12. {m}:匹配m个字符。
text = "abcd"
ret = re.match('\w{2}',text)
print(ret.group())
13. {m,n}:匹配m-n个字符
text = "abcdab"
ret = re.match('\w{1,5}',text)
print(ret.group())
############ 小案例 ############
14. 验证手机号码:
text = "12578900980"
ret = re.match('1[34578]\d{9}',text)
print(ret.group())
15. 验证邮箱:
text = "hynever12_@163com"
ret = re.match('\w+@[a-z0-9]+.[a-z]+',text)
print(ret.group())
16. 验证URL
text = "https://baike.baidu.com/item/Python/407313?fr=aladdin"
ret = re.match('(http|https|ftp)://[^\s]+',text)
print(ret.group())
17. 验证身份证:
text = "31131118908123230a"
ret = re.match('\d{17}[\dxX]',text)
print(ret.group())
18. ^(脱字号):
text = "hello"
ret = re.search('^h',text)
print(ret.group())
19. $:表示以...结尾:
text = "xxx@163.com"
ret = re.match('\w+@163.com$',text)
print(ret.group())
20. |:匹配多个字符串或者表达式:
text = "https"
ret = re.match('(ftp|http|https)$',text)
print(ret.group())
21:贪婪模式与非贪婪模式:
text = "0123456"
ret = re.match('\d+?',text)
print(ret.group())
text = "<h1>标题</h1>"
ret = re.match('<.+?>',text)
print(ret.group())
22:匹配0-100之间的数字
可以出现的:1,2,3,10,100,99
有三种情况:1,99,100
不可以出现的:09,101
text = "01"
ret = re.match('[1-9]\d?',text)
print(ret.group())
encoding: utf-8
import re
分组
text = "apple's price 10"
ret = re.search('.($\d+).($\d+)',text)
print(ret.group(0))
ret.group(0) = ret.group()
print(ret.group(1))
print(ret.group(2))
print(ret.group(1,2))
所有的子分组都拿出来
print(ret.groups())
find_all函数:
text = "apple's price 10"
ret = re.findall('$\d+',text)
print(ret)
sub函数:
text = "apple's price 10"
ret = re.sub('$\d+',"0",text)
print(ret)
html = """
<dd class="job_bt">
<h3 class="description">职位描述:</h3>
<div>
<p>参与公司新一代面向生命科学行业云服务应用及平台的开发。</p>
<p>
</p>
<p>【工作职责】</p>
<p>云服务软件产品的架构设计与开发</p>
<p>与设计、产品及前端人员沟通,保证产品的质量和开发进度</p>
<p>研究新兴技术,对产品进行持续优化</p>
<p>
</p>
<p>【职位要求】</p>
<p>计算机相关专业本科及以上学历</p>
<p>对常见数据结构和面向对象设计有深入理解</p>
<p>熟练掌握Python语言,3年以上实际经验</p>
<p>熟悉Python Web开发框架如Django</p>
<p>熟练掌握数据库开发和设计</p>
<p>基本的英文读写能力</p>
</div>
</dd>
"""
ret = re.sub('<.+?>',"",html)
print(ret)
split函数:
text = "hello&world ni hao"
ret = re.split('[^a-zA-Z]',text)
print(ret)
text = "the number is 20.50"
r = re.compile('\d+.?\d*')
r = re.compile(r"""
\d+ # 小数点前面的数字
.? # 小数点本身
\d* # 小数点后面的数字
""",re.VERBOSE)
ret = re.search(r,text)
print(ret.group())
网友评论