Re(详解版)

正则表达式语法


Python正则表达式

  • 指定好匹配的模式-pattern

  • 选择相应的方法-match,search等

  • 得到匹配结果-group

  • re.match #从开始位置开始匹配,如果开头没有则无

  • re.search #搜索整个字符串

  • re.findall #搜索整个字符串,返回一个list

input = '自然语言处理很重要 。 12abc789'
import re
pattern = re.compile(r'.')
re.findall(pattern,input)
['自',
 '然',
 '语',
 '言',
 '处',
 '理',
 '很',
 '重',
 '要',
 ' ',
 '。',
 ' ',
 '1',
 '2',
 'a',
 'b',
 'c',
 '7',
 '8',
 '9']

字符集合

  • [abc] 指定包含字符
  • [a-zA-Z] 来指定所以英文字母的大小写
  • [^a-zA-Z] 指定不匹配所有英文字母
pattern = re.compile(r'[abc]')
re.findall(pattern,input)
['a', 'b', 'c']
pattern = re.compile(r'[a-zA-Z]')
re.findall(pattern,input)
['a', 'b', 'c']
pattern = re.compile(r'[^a-zA-Z]')
re.findall(pattern,input)
['自',
 '然',
 '语',
 '言',
 '处',
 '理',
 '很',
 '重',
 '要',
 ' ',
 '。',
 ' ',
 '1',
 '2',
 '7',
 '8',
 '9']

或方法

将两个规则并列起来,以‘ | ’连接,表示只要满足其中之一就可以匹配。

  • [a-zA-Z]|[0-9] 表示满足数字或字母就可以匹配,这个规则等价于 [a-zA-Z0-9]
pattern = re.compile(r'[a-zA-Z]|[0-9]')
re.findall(pattern,input)
['1', '2', 'a', 'b', 'c', '7', '8', '9']

匹配数字 ‘\d’ 等价于 [0-9]

pattern = re.compile(r'\d')
re.findall(pattern,input)
['1', '2', '7', '8', '9']

‘\D’ 匹配非数字

pattern = re.compile(r'\D')
re.findall(pattern,input)
['自', '然', '语', '言', '处', '理', '很', '重', '要', ' ', '。', ' ', 'a', 'b', 'c']

‘\w’ 匹配字母和数字

pattern = re.compile(r'\w')
re.findall(pattern,input)
['自',
 '然',
 '语',
 '言',
 '处',
 '理',
 '很',
 '重',
 '要',
 '1',
 '2',
 'a',
 'b',
 'c',
 '7',
 '8',
 '9']

\W’ 匹配非字母和数字

pattern = re.compile(r'\W')
re.findall(pattern,input)
[' ', '。', ' ']

‘\s’ 匹配间隔符

pattern = re.compile(r'\s')
re.findall(pattern,input)
[' ', ' ']

重复

正则式可以匹配不定长的字符串

‘*’ 0 或多次匹配

pattern = re.compile(r'\d*')
re.findall(pattern,input)
['', '', '', '', '', '', '', '', '', '', '', '', '12', '', '', '', '789', '']

‘+’ 1 次或多次匹配

pattern = re.compile(r'\d+')
re.findall(pattern,input)
['12', '789']

‘?’ 0 或 1 次匹配

pattern = re.compile(r'\d?')
re.findall(pattern,input)
['',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '1',
 '2',
 '',
 '',
 '',
 '7',
 '8',
 '9',
 '']

精确匹配和最小匹配

‘{m}’ 精确匹配 m 次

pattern = re.compile(r'\d{3}')
re.findall(pattern,input)
['789']

{m,n}’ 匹配最少 m 次,最多 n 次。 (n>m)

pattern = re.compile(r'\d{1,3}')
re.findall(pattern,input)
['12', '789']

它们的返回不是一个简单的字符串列表,而是一个 MatchObject,可以得到更多的信息。

如果匹配不成功,它们则返回一个 NoneType 。所以在对匹配完的结果进行操作之前,必需先判断一下是否匹配成功了。

match 从字符串的开头开始匹配,如果开头位置没有匹配成功,就算失败了;而 search 会跳过开头,继续向后寻找是否有匹配的字符串。

input2 = '123自然语言处理'
pattern = re.compile(r'\d')
match = re.search(pattern,input2)
match.group()
'1'

字符串的替换和修改

在目标字符串中规格规则查找匹配的字符串,再把它们替换成指定的字符串。你可以指定一个最多替换次数,否则将替换所有的匹配到的字符串。

sub ( rule , replace , target [,count] )

subn(rule , replace , target [,count] )

第一个参数是正则规则,第二个参数是指定的用来替换的字符串,第三个参数是目标字符串,第四个参数是最多替换次数。

sub 返回一个被替换的字符串

subn 返回一个元组,第一个元素是被替换的字符串,第二个元素是一个数字,表明产生了多少次替换。

pattern = re.compile(r'\d')
re.sub(pattern,'数字',input2)
'数字数字数字自然语言处理'
pattern = re.compile(r'\d')
re.subn(pattern,'',input2)
('自然语言处理', 3)

split 切片函数。使用指定的正则规则在目标字符串中查找匹配的字符串,用它们作为分界,把字符串切片。

split( rule , target [,maxsplit] )

第一个参数是正则规则,第二个参数是目标字符串,第三个参数是最多切片次数,返回一个被切完的子字符串的列表

input3 = '自然语言处理123机器学习456深度学习'
pattern = re.compile(r'\d+')
re.split(pattern,input3)
['自然语言处理', '机器学习', '深度学习']

‘(?P…)’ 命名组

<…>’ 里面是你给这个组起的名字,

pattern = re.compile(r'(?P<dota>\d+)(?P<lol>\D+)')
m = re.search(pattern,input3)
m.group('lol')
'机器学习'

例子

# 筛选号码
input = 'number 338-343-220'
pattern = re.compile(r'(\d\d\d-\d\d\d-\d\d\d)')
m = re.search(pattern,input)

print(m.groups())

('338-343-220',)
posted @ 2021-07-06 12:13  风hua  阅读(521)  评论(0编辑  收藏  举报