正则表达式,又称规则表达式,英文名为Regular Expression,在代码中常简写为regex、regexp或RE,是计算机科学的一个概念。正则表通常被用来检索、替换那些符合某个模式(规则)的文本。 python 中正则表达式使用re模块。


文章目录

  • 1.使用 match()方法匹配字符串
  • 2.使用 search()在一个字符串中查找模式
  • 3.匹配多个字符
  • 4.匹配任何单个字符
  • 本关任务:编写代码,通过re.findall()模块匹配内容
  • Python 正则模块中常用的功能函数
  • finditer()函数;split()函数;sub()函数;subn()函数


1.使用 match()方法匹配字符串

match() 函数试图从字符串的起始部分对模式进行匹配。如果匹配成功,就返回一个匹配对象;如果 匹配失败,就返回 None,匹配对象的 group()方法能够用于显示那个成功的匹配。

import re
m = re.match('apple', 'apple on the table') 
m.group() 
------------------------
'apple'

记住,只能对起始部分进行匹配。

import re
m = re.match('table', 'apple on the table') 
if m is not None:
    m.group() 
else:
    print("can't find it")
-------------
can't find it

2.使用 search()在一个字符串中查找模式

import re
m = re.search('table', 'apple on the table') 
if m is not None:
    print(m.group()) 
else:
    print("can't find it")
 -------------
table

3.匹配多个字符

import re
bt='pen|on|table'
m = re.match(bt, 'apple') 
if m is not None:
    print(m.group()) 
else:
    print("can't find it")
m = re.match(bt, 'pen on the table') 
if m is not None:
    print(m.group()) 
else:
    print("can't find it")
m = re.search(bt, 'apple on the table') 
if m is not None:
    print(m.group()) 
else:
    print("can't find it")
m = re.search(bt, 'apple under the table') 
if m is not None:
    print(m.group()) 
else:
    print("can't find it")
m = re.match(bt, 'apple under the table') 
if m is not None:
    print(m.group()) 
else:
    print("can't find it")
-------------
can't find it
pen
on
table
can't find it

4.匹配任何单个字符

import re
anyend='.pple'
m = re.match(anyend, 'apple') #查找首字母后的字符
if m is not None:
    print(m.group()) 
else:
    print("can't find it")
m = re.search(anyend, 'i like apple') #查找首字母后的字符
if m is not None:
    print(m.group()) 
else:
    print("can't find it")
 -------------
apple
apple
import re
anyend='.end'
m = re.match(anyend, 'end') #不能匹配
if m is not None:
    print(m.group()) 
else:
    print("can't find it")
m = re.search(anyend, 'i like\nend') #换行后不能匹配
if m is not None:
    print(m.group()) 
else:
    print("can't find it")
 ------------
can't find it
can't find it
import re
anyend='.end'
m = re.search(anyend, 'in the end.') #试试这样
if m is not None:
    print(m.group())#输出结果的前面会有一个空格 
else:
    print("can't find it")
 ------------
 end

下面是真假“小数点”

import re
patt314 = '3.14'# 表示正则表达式的点号  
pi_patt = '3\.14'  # 表示字面量的点号 (dec. point) 
m = re.search(patt314, '3.14') 
if m is not None:
    print(m.group()) 
else:
    print("can't find it")
m = re.search(patt314, '3014') 
if m is not None:
    print(m.group()) 
else:
    print("can't find it")
m = re.search(patt314, '3514') 
if m is not None:
    print(m.group()) 
else:
    print("can't find it")
m = re.search(pi_patt, '3.14') 
if m is not None:
    print(m.group()) 
else:
    print("can't find it")
m = re.search(pi_patt, '3014') 
if m is not None:
    print(m.group()) 
else:
    print("can't find it")
------------
3.14
3014
3514
3.14
can't find it

本关任务:编写代码,通过re.findall()模块匹配内容

import re
text = input()
#********** Begin *********#
#1.匹配字符单词 Love
print(re.findall(r'Love',text))
#2.匹配以 w 开头的完整单词
print(re.findall(r'\bw\w*?\b',text))
#3.查找三个字母长的单词(提示:可以使用{m,n}方式)
print(re.findall(r'\b\w{3}\b',text))
#********** End **********#
Love your parents. We are too busy growing up yet we forget that they are already growing.

['Love']
['we']
['are', 'too', 'yet', 'are']

Python 正则模块中常用的功能函数

import re
text = input()
#********** Begin *********#
#1.用compile方法,匹配所有含字母i的单词
rr = re.compile(r'\w*i\w*')
print(rr.findall(text))
#2.在字符串起始位置匹配字符The是否存在,并返回被正则匹配的字符串
print(re.match('The',text).group())
#3.在整个字符串查看字符is是否存在,并返回被正则匹配的字符串
print(re.search('is',text).group())
#********** End **********#
There is a time in life that is full of uneasiness.We have no other choice but to face it.

['is', 'time', 'in', 'life', 'is', 'uneasiness', 'choice', 'it']
The
is

finditer()函数;split()函数;sub()函数;subn()函数

import re
text = input()
#********** Begin *********#
#1.匹配以t开头的所有单词并显示
itext = re.finditer( r'\bt\w*',text)
for i in itext:
    print(i.group())  
#2.用空格分割句子
print(re.split(r'\s',text))
#3.用‘-’代替句子中的空格 
print(re.sub(r'\s',r'-',text))
#4.用‘-’代替句子中的空格,并返回替换次数
print(re.subn(r'\s',r'-',text))
#********** End **********#
The moment you think about giving up, think of the reason why you held on so long.
think
think
the

['The', 'moment', 'you', 'think', 'about', 'giving', 'up,', 'think', 'of', 'the', 'reason', 'why', 'you', 'held', 'on', 'so', 'long.']
The-moment-you-think-about-giving-up,-think-of-the-reason-why-you-held-on-so-long.
('The-moment-you-think-about-giving-up,-think-of-the-reason-why-you-held-on-so-long.', 16)