当前位置:网站首页>速刷正则表达式一周目(上)
速刷正则表达式一周目(上)
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'
如果需要匹配真正的管道字符,就要用到转义字符。
附
正则表达式相对重要,推荐刷题网站:牛客网 - 找工作神器|笔试题库|面试经验|实习招聘内推,求职就业一站解决_牛客网
边栏推荐
- 聊聊 API 管理-开源版 到 SaaS 版
- FPGA工程师面试试题集锦11~20
- FPGA engineer interview questions collection 1~10
- pytorch框架学习(1)网络的简单构建
- Zhongang Mining: Strong downstream demand for fluorite
- Pony语言学习(六):Struct, Type Alias, Type Expressions
- 【LeetCode】41. The first missing positive number
- 大佬们,mysql cdc(2.2.1跟之前的版本)从savepoint起有时出现这种情况,有没有什
- SQL Server query optimization
- Concurrency tool class - introduction and use of CountDownLatch, CyclicBarrier, Semaphore, Exchanger
猜你喜欢
随机推荐
大咖说·对话生态|当Confluent遇见云:实时流动的数据更有价值
Hezhou ESP32C3 +1.8"tft network clock under Arduino framework
Get started with the OAuth protocol easily with a case
8.STM32F407之HAL库——PWM笔记
pytorch框架学习(6)训练一个简单的自己的CNN (三)细节篇
I have a dream for Career .
大佬们,mysql cdc(2.2.1跟之前的版本)从savepoint起有时出现这种情况,有没有什
Pulsar中游标的工作原理
Guys, is it normal that the oracle archive log grows by 3G in 20 minutes after running cdc?
Interface documentation evolution illustration, some ancient interface documentation tools, you may not have used it
并发工具类——CountDownLatch、CyclicBarrier、Semaphore、Exchanger的介绍与使用
聊聊 API 管理-开源版 到 SaaS 版
Thread.sleep, Thread.yield role explanation
scikit-learn机器学习 读书笔记(二)
ThreadPoolExecutor线程池原理
基于Qiskit——《量子计算编程实战》读书笔记(二)
Qiskit学习笔记(三)
PyTorch 入门之旅
通过一个案例轻松入门OAuth协议
How does flinksql write that the value of redis has only the last field?