学习python正则表达式

前言

学习的学习收获,本推送大部分采用书上的例子

关于python的字符意义

1:转义字符:


image.png

2:操作符


image.png

3:正则表达式相关


image.png

compile

正则表达式可以调用re模块,而 re.compile()可以传入一个字符串的值,用于构建匹配对象

Regex对象

我们可以利用search()函数来搜寻已传入的字符串

import re

phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d') #\d指0-9的数字
mo = phoneNumRegex.search('My number is 415-555-4242.') #查询传入的字符串
print('Phone number found: ' + mo.group()) # group()返回查询到符合条件的字符串
image.png

也可以利用括号分组,括号内显示你要分多少组

import re

phoneNumRegex = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)')
mo = phoneNumRegex.search('My number is 415-555-4242.')
print('Phone number found: ' + mo.group(1)) #选择第一组
image.png

还可以用管道匹配多个分组
这个嘛有点或者的感觉,只要匹配到第一个,就结束匹配了

#图1
import re

heroRegex = re.compile (r'Batman|Tina Fey')
mo1 = heroRegex.search('Batman and Tina Fey.')
print(mo1.group())

#图2
import re

heroRegex = re.compile (r'Batman|Tina Fey')
mo2 = heroRegex.search('Tina Fey and Batman.')
print(mo2.group())
图1
图2

利用?来进行可选匹配,即存在与否都可以匹配

#图3 
import re

batRegex = re.compile(r'Bat(wo)?man')
mo1 = batRegex.search('The Adventures of Batman')
print(mo1.group())

#图4
import re

batRegex = re.compile(r'Bat(wo)?man')
mo2 = batRegex.search('The Adventures of Batwoman')
print(mo2.group())

图3
图4

利用*来表示匹配0次或多次

#图5
import re

batRegex = re.compile(r'Bat(wo)*man')
mo1 = batRegex.search('The Adventures of Batman')
print(mo1.group())

#图6
import re

batRegex = re.compile(r'Bat(wo)*man')
mo2 = batRegex.search('The Adventures of Batwowowowoman')
print(mo2.group())
图5
图6

利用+来表示匹配1次或多次

#图7
import re

batRegex = re.compile(r'Bat(wo)+man')
mo1 = batRegex.search('The Adventures of Batman')
print(mo1.group())
#由于至少匹配一次,所以报错

#图8
import re

batRegex = re.compile(r'Bat(wo)+man')
mo2 = batRegex.search('The Adventures of Batwoman')
print(mo2.group())

#图9
import re

batRegex = re.compile(r'Bat(wo)+man')
mo3 = batRegex.search('The Adventures of Batwowowowowoman')
print(mo3.group())
图7 报错
图8
图9

利用花括号匹配指定次数

#图10
import re

haRegex = re.compile(r'(Ha){3}')
mo1 = haRegex.search('HaHa')
mo1.group()
print(mo1.group())

#图11
import re

haRegex = re.compile(r'(Ha){3}')
mo2 = haRegex.search('HaHaHa')
print(mo2.group())
图10 报错
图11

贪心匹配和非贪心匹配

正则表达式匹配一般都是贪心匹配,即匹配长的,多的
但是可以通过加一些符号使其变成非贪心匹配

我们进行匹配时:

import re

greedyHaRegex = re.compile(r'(Ha){3,5}') #花括号表示可以匹配3个或者5个
mo1 = greedyHaRegex.search('HaHaHaHaHa')
print(mo1.group())
image.png

计算机很贪心,它匹配最长的,也就是5个

如果在花括号后面加?,那么计算机采取非贪心匹配,匹配最短的

import re

nongreedyHaRegex = re.compile(r'(Ha){3,5}?')
mo1 = nongreedyHaRegex.search('HaHaHaHaHa')
mo1.group()
print(mo1.group())
image.png

findall()

search()和findall()的区别是,前者只返回第一次匹配的文本(match对象),即第一次满足条件退出;findall()是找到所有满足匹配条件的文本
并且search()要用group()来查看,findall()不用

#图12
import re

phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
mo = phoneNumRegex.search('Cell: 415-555-9999 Work: 212-555-0000')
print(mo.group())

#图13
import re

phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
m1 = phoneNumRegex.findall('Cell: 415-555-9999 Work: 212-555-0000')
print(m1)
图12
图13

插入字符与$符号

正则表达式 r'Hello'匹配以'Hello'开始的字符串,表示以什么什么开始的字符串

import re

phoneNumRegex = re.compile(r'^Hello')
mo = phoneNumRegex.search('Hello world!')
print(mo.group())
image.png

$匹配的是以什么什么结尾的文本

import re

phoneNumRegex = re.compile(r'\d$') #匹配以数字结尾的文本
mo = phoneNumRegex.search('Your number is 42')
print(mo.group())
image.png

那么^与$连用既是以什么什么开头,以什么什么结尾的文本

import re

phoneNumRegex = re.compile(r'^\d+$') #从头到尾都是数字
mo = phoneNumRegex.search('1234567890')
print(mo.group())
image.png

通配符

  1. "."句点通配符
    "."句点通配符表示除换行符以外的任意单个字符
import re

atRegex = re.compile(r'.at') #"."处可任意替换
mo = atRegex.findall('The cat in the hat sat on the flat mat.')
print(mo)
image.png

点-星匹配到第一个换行符前面的字符

import re

atRegex = re.compile('.*') #点-星匹配到第一个换行符前面的字符
mo = atRegex.search('Serve the public trust.\nProtect the innocent.\nUphold the law.')
print(mo.group())
image.png

若在后面加一个re.DOTALL,即匹配包括换行符的所有字符

import re

atRegex = re.compile('.*', re.DOTALL)
mo = atRegex.search('Serve the public trust.\nProtect the innocent.\nUphold the law.')
print(mo.group())
image.png

2.点-星匹配所有字符
分别匹配First Name和Last Name的所有内容

import re

nameRegex = re.compile(r'First Name: (.*) Last Name: (.*)') #分别匹配First Name和Last Name的所有内容
mo = nameRegex.search('First Name: Al Last Name: Sweigart')
print(mo.group())
image.png

贪心匹配和非贪心匹配

非贪心匹配(加?)

import re

nongreedyRegex = re.compile(r'<.*?>') #非贪心匹配<>内的文本
mo = nongreedyRegex.search(' for dinner.>')
print(mo.group())
image.png

贪心匹配(不加?)

import re

nongreedyRegex = re.compile(r'<.*>') #匹配所有的<>内的文本
mo = nongreedyRegex.search(' for dinner.>')
print(mo.group())
image.png

你可能感兴趣的:(学习python正则表达式)