当前位置:网站首页>速刷正则表达式一周目(上)
速刷正则表达式一周目(上)
2022-08-10 05:29:00 【我不叫内谁】
目录
前言:在文本查找方面,你可能很熟悉ctrlF,输入你想要查找的词。但正则表达式在此基础上更进一步,它可以通过位置、模式以及通配符等进行快速查找。本篇文章将以千字左右篇幅,带你速刷正则表达式的一些简单内容以及实战。
一、不使用正则表达式筛选电话号码
众所周知,电话号码由三部分组成,例如:132-3233-7777,是由3位网号+4位HLR号+4位的个人代码。假定我们构建一个函数,筛选广电号段的电话号码(中国广电号段:192),有如下操作
def phoneNumber(text):#1
if len(text) != 11:#2
return False
if text[0:3] != '192':#3
return False
for i in range(0,11):#4
if not text[i].isdecimal():#4
return False
return True
print(phoneNumber('12345557656'))
print(phoneNumber('19233333333'))首先#1使用def定义函数phoneNumber(),#2判断文本长度是否等于11位,若不等于,则返回False。#3遍历文本前三位,如果不等于192,则返回False,#4遍历全11位,如果不是纯数字,则返回False。
输出如下:

可以看到相当麻烦,接下来,以正则表达式查找文本。
二、使用正则表达式筛选电话号码
创建正则表达式对象
import re
phoneNumber = re.compile(r'1\d\d\d\d\d\d\d\d\d\d')
mo = phoneNumber.search('My number is 17543405207')
print("My phone number is " + mo.group())#变量名mo是一个通用的名称输出如下

复习:
第一步:使用import re导入正则表达式模块。
第二步:用re.compile()函数创建一个Regex对象(使用原始字符串)。
第三步:想Regex对象的search()方法传入想查找的字符串。它返回一个Match对象。
第四步:调用Match对象的group()方法,返回实际匹配文本的字符串。
三、利用括号分组
import re
phoneNumber = re.compile(r'(\d\d\d)-(\d\d\d\d-\d\d\d\d)')
mo = phoneNumber.search('My phone number is 175-4340-5207')
mo.group(1)
mo.group(0)
mo.group(2)输出如下

如果需要一次获取全部分组,使用groups()
四、用管道匹配多个分组
字符|被称为管道,希望匹配许多表达式中的一个时,可以有如下操作
color = re.compile(r'red|blue')
mo1 = color.search('red and blue')
mo1.group()
mo2 = color.search('blue and red')
mo2.group()如果希望匹配'Batman'、'Batmobile'、'Batcopter'
bat = re.compile(r'Bat(man|mobile|copter)')
mo = bat.search('Batmobile lost a wheel')
mo.group()
mo.group(1)dio用mo.group()返回完全匹配的文本'Batmobile',而mo.group(1)只是返回了第一个括号分组内的文本'mobile'
如果需要匹配真正的管道字符,就要用到转义字符。
附
正则表达式相对重要,推荐刷题网站:牛客网 - 找工作神器|笔试题库|面试经验|实习招聘内推,求职就业一站解决_牛客网
边栏推荐
- pytorch框架学习(9)torchvision.transform
- FPGA engineer interview questions collection 11~20
- FPGA工程师面试试题集锦11~20
- Mysql CDC (2.1.1) inital snapshot database set up five concurrent degree, se
- 看了几十篇轻量化目标检测论文扫盲做的摘抄笔记
- 从GET切换为POST提交数据的方法
- Shield Alt hotkey in vscode
- MySQL simple tutorial
- mysql cdc (2.1.1)inital snapshot数据库的时候设置了5个并发度,se
- summer preschool assignments
猜你喜欢

细数国产接口协作平台的六把武器!

pytorch框架学习(5)torchvision模块&训练一个简单的自己的CNN (二)

Practical skills 19: Several postures of List to Map List

聊聊 API 管理-开源版 到 SaaS 版

实战小技巧19:List转Map List的几种姿势

How to improve product quality from the code layer

What are the common commands of mysql

Read the excerpt notes made by dozens of lightweight target detection papers for literacy

【Pei Shu Theorem】CF1055C Lucky Days

YOLOv5 PyQt5(一起制作YOLOv5的GUI界面)
随机推荐
pytorch learning
The time for flinkcdc to read pgsql is enlarged. Does anyone know what happened? gmt_create':1
通过一个案例轻松入门OAuth协议
Ask you guys.The FlinkCDC2.2.0 version in the CDC community has a description of the supported sqlserver version, please
【Pei Shu Theorem】CF1055C Lucky Days
GtkD开发之路
Jenkins 如何玩转接口自动化测试?
pytorch框架学习(6)训练一个简单的自己的CNN (三)细节篇
Pony语言学习(七)——表达式(Expressions)语法(单篇向)
Flutter development: error The following assertion was thrown resolving an image codec: Solution for Unable to...
MongoDB 基础了解(一)
Practical skills 19: Several postures of List to Map List
Pony语言学习(一):环境配置(续)
Get started with the OAuth protocol easily with a case
深度梳理:防止模型过拟合的方法汇总
Pulsar中游标的工作原理
Joomla vulnerability reproduced
手把手带你写嵌入式物联网的第一个项目
基于BP神经网络的多因素房屋价格预测matlab仿真
AVL树的插入--旋转笔记