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

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

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

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

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

 

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

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

 

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

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

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

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

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

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

 

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

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

 

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

以上就是对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模式

2009-08-20 16:13:32

C#正则表达式匹配

2010-03-15 16:21:28

Python正则表达式

2023-04-11 08:54:57

字符串匹配算法

2023-12-15 10:27:01

暴力匹配算法Python字符串

2024-04-26 11:16:28

MySQL数据库

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

2010-07-26 10:51:26

Perl模式匹配

2023-10-30 10:20:45

2010-07-15 17:58:31

Perl模式

2015-11-23 10:07:19

Swift模式匹配

2010-07-26 11:02:19

Perl模式匹配

2009-07-08 14:22:36

Servlet容器匹配过程

2022-10-26 08:48:55

IT岗位产品经理

2024-07-01 08:40:18

tokio派生线程

2017-09-25 15:43:24

图像模板Python+Open

2021-04-28 11:40:13

正则表达式Regex前端
点赞
收藏

51CTO技术栈公众号