老男孩教育专注IT教育10余年,只培养IT技术精英

全国免费咨询电话(渠道合作):400-609-2893

Python学习教程之正则模块的使用

老男孩IT教育

技术博客

2021年7月27日 09:43

就其本质而言,正则表达式(或RE)是一种小型的、高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过re模块实现。正则表达式模式被编译成一系列的字节码,然后由用C编写的匹配引擎执行。

  就其本质而言,正则表达式(或RE)是一种小型的、高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过re模块实现。正则表达式模式被编译成一系列的字节码,然后由用C编写的匹配引擎执行。

Python学习教程

  元字符

  . :除换行符以外的任意符号,re.S模式也可以使 . 匹配包括换行在内的所有字符

  ^:匹配字符串的开头

  $:匹配字符串的末尾。

  *:匹配0个或多个的表达式。默认贪婪模式

  +:匹配1个或多个的表达式。默认贪婪模式

  ?:匹配0个或1个由前面的正则表达式,默认非贪婪模式

  { n,m}:匹配 n 到 m 次由前面的正则表达式定义的片段,贪婪方式

  [ ]:字符集,多个字符选其一,[^...]取反

  |:匹配做正则表达式或右边正则表达式

  ( ):G匹配括号内的表达式,也表示一个组

  \:转移符

import re

# (1) . ^ $
ret = re.findall("hello world","hello world")
print(ret)

ret = re.findall("^hello world$","hello python,hello world,hello re")
print(ret)

ret = re.findall("^hello .....$","hello world")
print(ret)

# (2) * + ?
ret = re.findall("^hello .*","hello ")
ret = re.findall("^hello .+","hello ")
ret = re.findall("^hello .?","hello abc")

# (3) {} ()
ret = re.findall("hello .{5}","hello python,hello world,hello re,hello yuan")
print(ret)
ret = re.findall("hello .{2,5}","hello python,hello world,hello re")
print(ret)
ret = re.findall("hello .{5},","hello python,hello world,hello re")
print(ret)
ret = re.findall("hello (.*?),","hello python,hello world,hello re,hello yuan,")
print(ret)
# ret = re.findall("hello (.*?)(?:,|$)","hello python,hello world,hello re,hello yuan")
# print(ret)

# (4) [] |
ret = re.findall("a[bcd]e","abeabaeacdeace")
print(ret)
ret = re.findall("[a-z]","123a45bcd678")
print(ret)
ret = re.findall("[^a-z]","123a45bcd678")
print(ret)
ret = re.findall("www\.([a-z]+)\.(?:com|cn)","www.baidu.com,www.jd.com")
print(ret)

# (5) \
'''
1、反斜杠后边跟元字符去除特殊功能,比如\.
2、反斜杠后边跟普通字符实现特殊功能,比如\d

    \d  匹配任何十进制数;      它相当于类 [0-9]。
    \D  匹配任何非数字字符;    它相当于类 [^0-9]。
    \s  匹配任何空白字符;      它相当于类 [ \t\n\r\f\v]。
    \S  匹配任何非空白字符;    它相当于类 [^ \t\n\r\f\v]。
    \w  匹配任何字母数字字符;   它相当于类 [a-zA-Z0-9_]。
    \W  匹配任何非字母数字字符; 它相当于类 [^a-zA-Z0-9_]
    \b  匹配一个特殊字符边界,比如空格 ,&,#等
'''

ret = re.findall("\d+","123a45bcd678")
print(ret)
ret = re.findall("(?:\d+)|(?:[a-z]+)","123a45bcd678")
print(ret)

  正则方法

import re

# 查找所有符合条件的对象
# re.findall() # 返回列表
# 查找第一个符合条件的匹配对象
s = re.search("\d+","a45bcd678")
print(s)
print(s.group())

# match同search,不过只在字符串开始处进行匹配
s = re.match("\d+","a45bcd678")
# print(s)
# print(s.group())

# 正则分割split
ret = re.split('[ab]', 'abcd')
print(ret)
# 正则替换
def func(match):

    name = match.group()
    print("name",name)
    return "xxx"

# \1代指第一个组匹配的内容  \2第二个组匹配的内容,思考如何能将所有的名字转大写替换
ret = re.sub("(hello )(.*?)(,)","\\1yuan\\3","hello python,hello world,hello re,")
print("ccc",ret)

# 编译再执行
obj=re.compile('\d{3}')
ret=obj.search('abc123ee45ff')
print(ret.group()) # 123

  练习:爬虫豆*网

com=re.compile(
    '<div class="item">.*?<div class="pic">.*?<em .*?>(?P<id>\d+).*?<span class="title">(?P<title>.*?)</span>'
    '.*?<span class="rating_num" .*?>(?P<rating_num>.*?)</span>.*?<span>(?P<comment_num>.*?)评价</span>',
    re.S)

com.findall(s)

  推荐阅读:

  【总结】Python基础面试题30问!Python基础教程

  Python匿名函数:lambda表达式的使用!

  Python入门学习教程之seek与tell方法!

本文经授权发布,不代表老男孩教育立场。如若转载请联系原作者。