Python匹配如何才能完成匹配细节

开发 后端
Python匹配在使用的时候有不少的知识需要我们学习,下面我们就详细的来了解下相关的知识。只有这样在之后的使用中才能更顺手。

Python匹配在我们使用的时候有很多的注意事项。我们在不断的学习中会遇到不少的问题。下面我们就详细的看看如何才能更好的掌握相关的Python匹配技术问题。用法2的正则表达式对象版本

  1. rereobj = re.compile(r"\Z") #正则表达式末尾以\Z 结束  
  2. if reobj.match(subject):  
  3. do_something()  
  4. else:  
  5. do_anotherthing() 

创建一个正则表达式对象,然后通过该对象获得Python匹配细节

 

  1. rereobj = re.compile(regex)  
  2. match = reobj.search(subject)  
  3. if match:  
  4. # match start: match.start()  
  5. # match end (exclusive): match.end()  
  6. # matched text: match.group()  
  7. do_something()  
  8. else:  
  9. do_anotherthing() 

用正则表达式对象获取Python匹配子串

 

  1. rereobj = re.compile(regex)  
  2. match = reobj.search(subject)  
  3. if match:  
  4. result = match.group()  
  5. else:  
  6. result = "" 

用正则表达式对象获取 捕获组所Python匹配的子串

  1. rereobj = re.compile(regex)  
  2. match = reobj.search(subject)  
  3. if match:  
  4. result = match.group(1)  
  5. else:  
  6. result = "" 

用正则表达式对象获取 有名组所Python匹配的子串

  1. rereobj = re.compile(regex)  
  2. match = reobj.search(subject)  
  3. if match:  
  4. result = match.group("groupname")  
  5. else:  
  6. result = "" 

用正则表达式 对象获取所有Python匹配子串并放入数组

 

  1. rereobj = re.compile(regex)  
  2. result = reobj.findall(subject) 

通过正则表达式对象遍历所Python有匹配子串

 

  1. rereobj = re.compile(regex)  
  2. for match in reobj.finditer(subject):  
  3. # match start: match.start()  
  4. # match end (exclusive): match.end()  
  5. # matched text: match.group 

以上就是对Python匹配的相关细节介绍。

【编辑推荐】

  1. Python脚本解决在游戏开发中的困难
  2. Python脚本如何保证游戏正常开发
  3. Python字符串替换如何才能进行字符的拆分
  4. Python编程语言总体性能优点评测
  5. Python编程语言维和受到众人的追捧
责任编辑:张浩 来源: CSDN
相关推荐

2009-09-16 18:08:14

正则表达式匹配单词

2022-08-29 15:26:58

MySQLSQL模式

2010-03-15 16:21:28

Python正则表达式

2009-08-20 16:13:32

C#正则表达式匹配

2023-12-15 10:27:01

暴力匹配算法Python字符串

2023-04-11 08:54:57

字符串匹配算法

2021-01-18 05:18:18

C# 8模式C# 7

2011-03-15 15:20:46

2010-03-18 12:57:46

python(V1.0

2009-07-08 14:22:36

Servlet容器匹配过程

2010-07-26 11:02:19

Perl模式匹配

2023-10-30 10:20:45

2010-07-26 10:51:26

Perl模式匹配

2010-07-15 17:58:31

Perl模式

2015-11-23 10:07:19

Swift模式匹配

2022-10-26 08:48:55

IT岗位产品经理

2017-09-25 15:43:24

图像模板Python+Open

2021-04-28 11:40:13

正则表达式Regex前端

2010-07-21 13:27:06

Perl模式匹配

2021-03-10 07:20:44

数据定位匹配
点赞
收藏

51CTO技术栈公众号