关于python的正则表达式的信息

[img]

简介:

正则表达式是指按照某种规则去匹配符合要求的字符串的方法。在编程领域中,正则表达式是一种重要的技能,Python自带的re模块提供了一组函数,用来对字符串进行正则表达式的解释和匹配。

多级标题:

一、基础用法

二、匹配字符

三、特殊字符

四、匹配次数

五、贪婪与非贪婪

六、re模块常用函数

内容详细说明:

一、基础用法:

Python的re模块中最常用的函数是search()和match()函数。其中,match()函数仅匹配字符串开头,若需匹配整个字符串,需要使用search()函数。

例如:

import re

pattern = re.compile('hello')

result1 = pattern.match('hello world')

result2 = pattern.match('hi world')

result3 = pattern.search('say hello to the world')

print(result1)

print(result2)

print(result3)

输出结果:

<_sre.SRE_Match object; span=(0, 5), match='hello'>

None

<_sre.SRE_Match object; span=(4, 9), match='hello'>

二、匹配字符:

正则表达式中使用元字符匹配字符。元字符包括:点号(.)、竖线(|)、圆括号()、方括号[]、脱字符(^)、美元符号($)等。

例如:

import re

pattern1 = re.compile('he.')

pattern2 = re.compile('he|she')

pattern3 = re.compile('(hello){2}')

pattern4 = re.compile('[abc]')

pattern5 = re.compile('[^abc]')

pattern6 = re.compile('^\d')

pattern7 = re.compile('\d$')

result1 = pattern1.findall('hello world')

result2 = pattern2.findall('I love she, and she loves me')

result3 = pattern3.findall('hellohello')

result4 = pattern4.findall('boring job')

result5 = pattern5.findall('this is a mobile phone')

result6 = pattern6.findall('8 am')

result7 = pattern7.findall('black13')

print(result1)

print(result2)

print(result3)

print(result4)

print(result5)

print(result6)

print(result7)

输出结果为:

['hel']

['he', 'she', 'she']

['hellohello']

['b', 'o', 'r', 'i', 'n', 'g', 'j', 'o', 'b']

['t', 'h', 'i', 's', ' ', 'i', 's', ' ', 'm', 'o', 'i', 'l', 'e', ' ', 'p', 'h', 'o', 'n', 'e']

['8']

['3']

三、特殊字符:

正则表达式中,有些元字符是具有特殊意义的字符。例如:

\d,匹配一个数字字符;

\w,匹配一个单词字符;

\s,匹配一个空白字符;

\D,匹配一个非数字字符;

\W,匹配一个非单词字符;

\S,匹配一个非空白字符。

例如:

import re

pattern1 = re.compile('\d+')

pattern2 = re.compile('\w')

pattern3 = re.compile('\s')

result1 = pattern1.findall('there are 13 apples')

result2 = pattern2.findall('hello-world master')

result3 = pattern3.findall('hi, I am here')

print(result1)

print(result2)

print(result3)

输出结果为:

['13']

['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd', 'm', 'a', 's', 't', 'e', 'r']

[' ', ' ', ' ', ' ', ' ']

四、匹配次数:

正则表达式中,我们可以通过使用元字符来控制需要匹配的字符的数目。元字符包括:星号(*)、加号(+)、问号(?)、大括号({m,n})等。

例如:

import re

pattern1 = re.compile('\d*')

pattern2 = re.compile('\d+')

pattern3 = re.compile('\d?')

pattern4 = re.compile('\d{3,4}')

result1 = pattern1.findall('1234')

result2 = pattern2.findall('hello1234world56')

result3 = pattern3.findall('1 2 23 456')

result4 = pattern4.findall('021-98765433')

print(result1)

print(result2)

print(result3)

print(result4)

输出结果为:

['', '1', '2', '3', '4', '']

['1234', '56']

['1', '', '2', '', '', '2', '3', '', '4', '5', '6', '']

['021-', '9876']

五、贪婪与非贪婪:

正则表达式默认是贪婪模式,即匹配最长的符合要求的字符串。如果要使用非贪婪模式,需要在元字符的后面加上一个问号(?)。

例如:

import re

pattern1 = re.compile('a.*c')

pattern2 = re.compile('a.*?c')

result1 = pattern1.findall('abaccado')

result2 = pattern2.findall('abaccado')

print(result1)

print(result2)

输出结果为:

['abacc']

['abac']

六、re模块常用函数:

1. match()函数,从字符串开头进行匹配。

例如:pattern.match('hello')

2. search()函数,全局匹配。

例如:pattern.search('hello world')

3. findall()函数,返回所有匹配的字符串列表。

例如:pattern.findall('hello world, hello python')

4. sub()函数,将匹配的部分替换成新的字符串。

例如:re.sub(pattern, new_str, old_str)

总结:

正则表达式是Python编程中非常重要的部分,掌握基本的正则语法以及常用函数,可以为我们的编程工作提供很大的便利。

标签列表