Python正则表达式的几种匹配用法

下面列出: 1.测试正则表达式是否匹配字符串的全部或部分regex=ur"" #正则表达式

if re.search(regex, subject): do_something()else: do_anotherthing()

2.测试正则表达式是否匹配整个字符串 regex=ur"/z" #正则表达式末尾以/z结束

if re.match(regex, subject): do_something()else: do_anotherthing()

3.创建一个匹配对象,然后通过该对象获得匹配细节(create an object with details about how the regex matches (part of) a string) regex=ur"" #正则表达式

match = re.search(regex, subject)if match: # match start: match.start() # match end (exclusive): atch.end() # matched text: match.group() do_something()else: do_anotherthing()

4.获取正则表达式所匹配的子串(get the part of a string matched by the regex) regex=ur"" #正则表达式

match = re.search(regex, subject)if match: result = match.group()else: result =""

5. 获取捕获组所匹配的子串(get the part of a string matched by a capturing group) regex=ur"" #正则表达式

match = re.search(regex, subject)if match: result = match.group(1)else: result =""

6. 获取有名组所匹配的子串(get the part of a string matched by a named group) regex=ur"" #正则表达式

match = re.search(regex, subject)if match:result = match.group"groupname")else:result = ""

7. 将字符串中所有匹配的子串放入数组中(get an array of all regex matches in a string) result = re.findall(regex, subject)

8.遍历所有匹配的子串(iterate over all matches in a string) for match in re.finditer(r"<(.*?)/s*.*?//1>", subject) # match start: match.start() # match end (exclusive): atch.end() # matched text: match.group()

9.通过正则表达式字符串创建一个正则表达式对象(create an object to use the same regex for many operations) reobj = re.compile(regex)

10.用法1的正则表达式对象版本(use regex object for if/else branch whether (part of) a string can be matched) reobj = re.compile(regex)if reobj.search(subject): do_something()else: do_anotherthing()

11.用法2的正则表达式对象版本(use regex object for if/else branch whether a string can be matched entirely) reobj = re.compile(r"/z") #正则表达式末尾以/z 结束

if reobj.match(subject): do_something()else: do_anotherthing()

12.创建一个正则表达式对象,然后通过该对象获得匹配细节(create an object with details about how the regex object matches (part of) a string) reobj = re.compile(regex) match = reobj.search(subject)if match: # match start: match.start() # match end (exclusive): atch.end() # matched text: match.group() do_something()else: do_anotherthing()

13.用正则表达式对象获取匹配子串(use regex object to get the part of a string matched by the regex) reobj = re.compile(regex) match = reobj.search(subject)if match: result = match.group()else: result =""

14.用正则表达式对象获取捕获组所匹配的子串(use regex object to get the part of a string matched by a capturing group) reobj = re.compile(regex) match = reobj.search(subject)if match: result = match.group(1)else: result =""

15.用正则表达式对象获取有名组所匹配的子串(use regex object to get the part of a string matched by a named group) reobj = re.compile(regex) match = reobj.search(subject)if match: result = match.group("groupname")else: result =""

16.用正则表达式对象获取所有匹配子串并放入数组(use regex object to get an array of all regex matches in a string) reobj = re.compile(regex) result = reobj.findall(subject) 17.通过正则表达式对象遍历所有匹配子串(use regex object to iterate over all matches in a string) reobj = re.compile(regex)for match in reobj.finditer(subject): # match start: match.start() # match end (exclusive): match.end() # matched text: match.group()字符串替换

1.替换所有匹配的子串 #用newstring替换subject中所有与正则表达式regex匹配的子串

result = re.sub(regex, newstring, subject)

2.替换所有匹配的子串(使用正则表达式对象) reobj = re.compile(regex) result = reobj.sub(newstring, subject) 字符串拆分

1.字符串拆分 result = re.split(regex, subject)

2.字符串拆分(使用正则表示式对象) reobj = re.compile(regex) result = reobj.split(subject)

原文地址:https://www.weidianyuedu.com/content/5720628093308.html

Python正则表达式的几种匹配用法相关推荐

  1. Python正则表达式常用flag含义与用法详解

    封面图片:<Python程序设计实验指导书>(ISBN:9787302525790),董付国,清华大学出版社 图书详情(京东): ================== Python正则表达 ...

  2. Python正则表达式\W+和\W*匹配过程的深入分析

    在学习re.split函数的处理过程中,发现执行如下语句及返回与老猿预想的不一致: >>> re.split('\W*','Hello,world') ['', 'H', 'e', ...

  3. 正则表达式的3种匹配模式

    正则表达式是处理字符串的常用工具.在C#中,我们一般使用Regex类来表示一个正则表达式.一般正则表达式引擎支持以下3种匹配模式:单行模式(Singleline).多行模式(Multiline)与忽略 ...

  4. php正则表达式的匹配,php正则表达式的3种匹配模式

    正则表达式是处理字符串的常用工具.在C#中,我们一般使用Regex类来表示一个正则表达式.一般正则表达式引擎支持以下3种匹配模式:单行模式(Singleline).多行模式(Multiline)与忽略 ...

  5. python正则表达式的几个匹配函数用法

    1.finditer 匹配字符串中所有符合正则的内容,返回的是迭代器,从迭代器拿内容需要用到.group() it = re.finditer(r"匹配条件","需要匹配 ...

  6. Python正则表达式: 元字符/转义/分组/匹配原则/re模块属性方法大全

    正则表达式 动机 文本处理已经成为计算机常见工作之一 对文本内容的搜索,定位,提取是逻辑比较复杂的工作 为了快速方便的解决上述问题,产生了正则表达式技术 简介 定义 即文本的高级匹配模式,提供搜索,替 ...

  7. python中的几种copy用法_Python3中copy模块常用功能及其他几种copy方式比较

    1.简单的共享引用: python中内置有小整数常量池和字符串常量池,在某个范围内的相同的数字或字符串分别赋给不同的变量,这些不同的变量都是指向同一块内存地址,这就是所谓的共享引用,举几个简单的例子: ...

  8. Python正则表达式尽可能小的匹配(遇到第一个结束字符串就停止匹配)

    在写爬虫爬网页的时候,经常需要爬取里面的一大块代码,比如: <div>..................................</div></div>& ...

  9. Python正则表达式如何进行字符串替换实例

    Python正则表达式如何进行字符串替换实例 Python正则表达式在使用中会经常应用到字符串替换的代码.有很多人都不知道如何解决这个问题,下面的代码就告诉你其实这个问题无比的简单,希望你有所收获. ...

最新文章

  1. c# socket接收字符串_Python高级编程之 Socket 编程
  2. dell系统重装后无法进入系统_笔记本电脑常见故障开机无法进入系统
  3. angularjs 上传
  4. 进程管理—进程描述符(task_struct)
  5. php new static 效率,PHP中new static()与new self()的比较
  6. mysql中的正向工程_Hibernate系列之正向工程
  7. note_maven的pom.xml部分配置说明
  8. 高校学生学籍系统C++amp;mysql
  9. 集群启动/停止方式总结
  10. Centos 进入单入口模式
  11. 画像在同城物流调度系统的实践
  12. Gradle报 skkiped gradle skipped due to earlier error
  13. Fastdb安装与使用
  14. unity Color和Hex转化
  15. ECN Trade:飓风灾害短暂冲击美国经济
  16. php 知乎源代码,PHP最新仿知乎问答社区源码下载带行业打赏问答支持文章、话题、第三方登录、文章和问题打赏...
  17. 微信如何查看是否被删好友,这4种方法简单快捷!
  18. markdown编辑器的基本使用
  19. 经验分享:微信小程序外包接单常见问题及流程 1
  20. SpringMVC框架理解

热门文章

  1. JTS-WKB格式输入输出使用说明(十七)
  2. 你是否真的需要MacBook?Windows和Mac OS怎么选?
  3. STC15F104W 点亮一个LED 上手初测
  4. 常见的运行时异常。(Java)
  5. 关于Jetbrains学生账号无法登录验证的解决方案
  6. 神经网络实战记录11—调参技巧2—fine-tune(基于VGGNet tensorboard代码改)
  7. linux 774是什么权限,Linux 权限位,权限值,权限管理
  8. 如何一键抠图?手把手教你抠图
  9. 成都大学计算机学院有没有大专,成都计算机专科大学
  10. 上市首日股价大跌,美国餐饮SaaS第一股Olo不够“香”吗?