python模块:re模块

古城微笑少年丶 2023-10-11 19:16 171阅读 0赞

  . 匹配任意字符

  [] 匹配指定字符类别

  ^ 字符开头

  $ 字符结尾

  [^] 取非字符

  * 重复多次字符(0次或多次)

  + 重复多次字符(1次或多次)

  ? 重复单次字符

  | 左右表达式任意匹配

  {m,n} 重复m到n次字符

  {m} 重复m次字符

  \d 匹配任何十进制数,相当于[0-9]

  \D 匹配任何非数字字符,相当于[^0-9]

  \s 匹配任何空白字符,相当于[fdss]

  \S 匹配任何非空白字符,相当于[^jdvnjsd]

  \w 匹配任何数字字母,相当于[a-zA-Z0-9]

  \W 匹配任何非数字字母,相当于[^a-zA-Z0-9]

  例1:定义简单的正则表达式

  格式:re.compile(strPattern[, flag]):

  strPattern:字符串表达式

  flag:

  re.I(re.IGNORECASE): 忽略大小写(括号内是完整写法,下同)

  M(MULTILINE): 多行模式,改变’^’和’$’的行为

  S(DOTALL): 点任意匹配模式,改变’.’的行为

  L(LOCALE): 使预定字符类 \w \W \b \B \s \S 取决于当前区域设定

  U(UNICODE): 使预定字符类 \w \W \b \B \s \S \d \D 取决于unicode定义的字符属性

  X(VERBOSE): 详细模式。这个模式下正则表达式可以是多行,忽略空白字符,并可以加入注释。

  pattern=re.compile(r’heLLow’,re.I) #生成一个pattern实例

  match=pattern.match(‘hellow world’) #使用pattern匹配文本

  if match:

  print match.group() #如果匹配就输出

  #例2:match属性和方法

  #!/bin/env python

  #!-*- coding:UTF-8 -*-

  import re

  match=re.match(r’(\w+)(\w+)(?P .*)’,’hellow world!’) #使用pattern匹配文本

  print “match.string:”,match.string #匹配时使用的文本

  print “match.re:”,match.re #匹配时使用的pattern对像

  print “match.pos:”,match.pos #开始搜索的索引

  print “match.endpos:”,match.endpos #结束搜索的索引

  print “match.lastindex:”,match.lastindex #最后一个被捕获在分组在文本中的索引

  print “match.lastgroup”,match.lastgroup #最后一个被捕获的分组名

  print “match.group(1,2):”,match.group(1,2) #获得一个或多个分组截获的字符串

  print “match.groups():”,match.groups() #以元组形式返回全部分组的字符串

  print “match.groupdict():”,match.groupdict() #返回有别名组的别名为键

  print “match.start(2):”,match.start(2) #返回指定组截获的子串在字符中的起始索引

  print “match.end(2):”,match.end(2) #返回指定组截获的子串在字符中的结束索引

  print “match.span(2):”,match.span(2) #返回起始组和结束组

  #例3:re模块和方法

  re.compile #转换为Pattern对像

  re.match #匹配正零时表达式

  re.search #查找字符串

  re.split #分割字符串

  re.findall #搜索字符中,以列表形式返回全部能匹配的子串

  re.finditer #搜索字符串,返回一个顺序访问每一个匹配的结果

  re.sub #替换字符串

  re.subn #替换字符串,n次

  #例4:查找字符串

  a=re.compile(r’hello’)

  b=a.search(‘hello world’) #查找a中是否有hello字符

  if b:

  print b.group()

  #例5:截断字符串

  p=re.compile(r’\d’)

  print p.findall(‘one1two2three3four4five5’)

  #例6:返回每个匹配的结果

  w=re.compile(r’\d’)

  for m in w.finditer(‘one1two2three3four4five5’):

  print m.group()

  #例7:

  e=re.compile(r’(\w+)(\w+)’)

  s=’This is, tong cheng’

  print e.sub(r’\2\1’,s)

  def func(m):

  return m.group(1).title()+ ‘ ‘ + m.group(2).title()

  print e.sub(func,s)

发表评论

表情:
评论列表 (有 0 条评论,171人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Python应用】re模块

    简介 re模块用来操作正则表达式。 具体来说是利用正则表达式,找到特定字符串中特定内容(就是用正则表达式表表示的内容)的组织形式。 本文主要参考[6.2. re —

    相关 pythonre模块

    re python中re模块提供了正则表达式相关操作 字符: . 匹配除换行符以外的任意字符 加匹配模式 re.S 后.可以匹配任意字符 \w

    相关 re模块

    正则表达式是对字符串进行模糊匹配,字符串的方法是绝对匹配或完全匹配 re用c语言编写的匹配引擎执行,效率高 字符匹配(普通字符,元字符),元字符提供了很多模糊匹配的可能

    相关 re模块

    一、简述   就其本质而言,正则表达式(或 RE)是一种小型的、高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现。你可以为想要匹配...