系统运维

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

字号+作者:益华科技来源:数据库2025-11-04 21:53:28我要评论(0)

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

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

  元字符

  . :除换行符以外的使用任意符号,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、WordPress模板反斜杠后边跟元字符去除特殊功能,比如\. 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)免费信息发布网

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 如何去掉Word文档中的空白页?(简单有效的方法帮助您轻松处理Word文档中的无用空白页)

    如何去掉Word文档中的空白页?(简单有效的方法帮助您轻松处理Word文档中的无用空白页)

    2025-11-04 21:36

  • windows 7更新Windows Update报80070005错误的解决办法

    windows 7更新Windows Update报80070005错误的解决办法

    2025-11-04 20:32

  • windows 7卸载KB2952664/KB3035583补丁的详细图文教程

    windows 7卸载KB2952664/KB3035583补丁的详细图文教程

    2025-11-04 19:41

  • windows 7怎么更换扫雷游戏的外观让其与众不同

    windows 7怎么更换扫雷游戏的外观让其与众不同

    2025-11-04 19:10

网友点评