代码编织梦想

正则表达式使用方法:

1> 用import re导入正则表达式模块

2> 用re.compile()函数创建一个正则表达式对象(记得使用原始字符串)

3> 向Regex对象的search()方法传入想查找的字符串。它返回一个Match对象

4> 调用Match对象的group()方法,返回实际匹配文本的字符串

import re
phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
mo = phoneNumRegex.search('my number is 425-444-3467')
print('phone number found ' + mo.group())
======================================================
result:
phone number found 425-444-3467

利用括号分组:

import re
phoneNumRegex = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)')
mo = phoneNumRegex.search('my number is 425-444-3467')
print(mo.group())
print(mo.group(1))
print(mo.group(2))
========================================================
result:
425-444-3467
425
444-3467

用管道匹配多个分组:希望匹配多个表达式中的一个时,可以使用管道

import re
heroRegex = re.compile(r'Batman|Tina Fey')
mo = heroRegex.search('Batman and Tina Fey')
print(mo.group())
mo = heroRegex.search('Tina Fey and Batman')
print(mo.group())
=============================================
result:
Batman
Tina Fey
import re
batRegex = re.compile(r'Bat(man|mobile|copter|bat)')
mo = batRegex.search('Batcopter lost a wheel')
print(mo.group())
print(mo.group(1))
====================================================
result:
Batcopter
copter

用问号实现可选匹配:?表示出现0次或一次

import re
batRegex = re.compile(r'Bat(wo)?man')
mo = batRegex.search('Batman lost a wheel')
print(mo.group())
mo = batRegex.search('Batwoman lost a wheel')
print(mo.group())
=============================================
result:
Batman
Batwoman

用星号匹配零次或多次:

import re
batRegex = re.compile(r'Bat(wo)*man')
mo = batRegex.search('Batman lost a wheel')
print(mo.group())
mo = batRegex.search('Batwoman lost a wheel')
print(mo.group())
mo = batRegex.search('Batwowowowoman lost a wheel')
print(mo.group())
===================================================
result:
Batman
Batwoman
Batwowowowoman

用加号匹配一次或多次:

import re
batRegex = re.compile(r'Bat(wo)+man')
mo = batRegex.search('Batman lost a wheel')
print(mo)
mo = batRegex.search('Batwoman lost a wheel')
print(mo.group())
mo = batRegex.search('Batwowowowoman lost a wheel')
print(mo.group())
==================================================
result:
None
Batwoman
Batwowowowoman

用花括号匹配特定次数:

import re
haRegex = re.compile(r'(Ha){2,3}')
mo = haRegex.search('Ha')
print(mo)
mo = haRegex.search('HaHa')
print(mo.group())
mo = haRegex.search('HaHaHa')
print(mo.group())
mo = haRegex.search('HaHaHaHa')
print(mo.group())
===============================
result:
None
HaHa
HaHaHa
HaHaHa

贪心和非贪心匹配:python默认是贪心匹配,eg:(Ha){3,5}默认以匹配更多的实例为准,可在{3,5}后加?表示使用非贪心匹配

import re
haRegex = re.compile(r'(Ha){2,3}?')
mo = haRegex.search('Ha')
print(mo)
mo = haRegex.search('HaHa')
print(mo.group())
mo = haRegex.search('HaHaHa')
print(mo.group())
mo = haRegex.search('HaHaHaHa')
print(mo.group())
==================================
result:
None
HaHa
HaHa
HaHa

findall方法:

import re
phoneNumRegex = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)')
mo = phoneNumRegex.search('person01: 425-444-3467, person02: 425-678-4678')
print(mo.group())
mo = phoneNumRegex.findall('person01: 425-444-3467, person02: 425-678-4678')
print(mo)
============================================================================
result:
425-444-3467
[('425', '444-3467'), ('425', '678-4678')]

字符分类:

import re
# 至少一个数字+空格+一个字母
phoneNumRegex = re.compile(r'\d+\s\w+')
mo = phoneNumRegex.findall('8 c, 7 rrr, qqg, 19 tt')
print(mo)
====================================================
result:
['8 c', '7 rrr', '19 tt']

建立自己的字符分类:在缩写的\d \s \w太宽泛的情况下可以自定义字符集

import re
phoneNumRegex = re.compile(r'[aeiou]')
mo = phoneNumRegex.findall('Hello world')
print(mo)
=========================================
result:
['e', 'o', 'o']

插入字符和美元字符:

1> 插入字符^表示以字符串开头的匹配

2> 美元字符$表示以字符串结束的匹配

3> 同时使用^$字符表示整个子串必须匹配模式,如r'^\d+$'表示全是数字

import re
beginRegex = re.compile(r'^Hello')
mo = beginRegex.findall('Hello world')
print(mo)
endRegex = re.compile(r'\d$')
mo = endRegex.findall('Hello world')
print(mo)
mo = endRegex.findall('Hello world4')
print(mo)
beginEndRegex = re.compile(r'^\d+$')
mo = beginEndRegex.findall('45y889')
print(mo)
mo = beginEndRegex.findall('45889')
print(mo)
====================================
result:
['Hello']
[]
['4']
[]
['45889']

通配字符:.表示匹配除了换行以外的任意字符

import re
beginRegex = re.compile(r'.at')
mo = beginRegex.findall('The cat in the hat sat on the first mat')
print(mo)
==================================================================
result:
['cat', 'hat', 'sat', 'mat']

用.*匹配所有字符:

import re
beginRegex = re.compile(r'First Name:(.*) Last Name:(.*)')
mo = beginRegex.search('First Name:Broad Last Name:Cast')
print(mo.group(1))
print(mo.group(2))
==========================================================
result:
Broad
Cast

正则的第二个参数:

1> DOTALL:全部字符,包括换行

2> IGNORECASE:忽略大小写

3> VERBOSE:忽略空白符和注释

import re
beginRegex = re.compile(r'(.*)last', re.DOTALL | re.IGNORECASE | re.VERBOSE)
mo = beginRegex.search('First Name:Broad Last Name:Cast'
                       'sdf  sdf fs dffsdf dfdfasdf ')
print(mo.group())
===========================================================================
result:
First Name:Broad Last

正则表达式做替换:sub()函数有两个参数,第一个参数用于取代发现的匹配字符串,第二个参数是匹配的内容

import re
agentRegex = re.compile(r'Agent \w+')
mo = agentRegex.sub('Agent xx', 'Agent Alice gave the secret documents to Agent Bob')
print(mo)
====================================================================================
result:
Agent xx gave the secret documents to Agent xx

  ====================================================================

博主有自己的个人主页啦!请求关注,请求支持。QAQ.    yc.hi.cn

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接: https://blog.csdn.net/SuperYang_/article/details/111063778

Python爬虫:短视频平台无水印下载!强不强?-爱代码爱编程

导入: 虽然目前有些软件还没适配,但是,我发了 Blink 后有一写人留言或者私信找我要源码,不过我还在增加适配的软件,所以还没有时间写这篇博客,今天呢,就先把我目前适配了的代码拿出来,后续还会继续适配的! 分平台解释: 皮皮虾 皮皮搞笑 皮皮搞笑与皮皮虾很类似,也是先获取分享链接,在电脑端进行分析: PS:如有需要Python学习资料的小伙伴

Python爬虫:短视频平台无水印下载!强不强?-爱代码爱编程

导入: 虽然目前有些软件还没适配,但是,我发了 Blink 后有一写人留言或者私信找我要源码,不过我还在增加适配的软件,所以还没有时间写这篇博客,今天呢,就先把我目前适配了的代码拿出来,后续还会继续适配的! 分平台解释: 皮皮虾 皮皮搞笑 皮皮搞笑与皮皮虾很类似,也是先获取分享链接,在电脑端进行分析: PS:如有需要Python学习资料的小伙伴

剑指Offer 19. 正则表达式匹配(Hard)-爱代码爱编程

44. 通配符匹配(Hard)【题目链接】题解 正则表达式匹配(动态规划,清晰图解)字节题库 - #剑19 - 困难 - 正则表达式匹配思路 代码 class Solution: ### 1211 动态规划(68 ms,14.8 MB) def isMatch(self, s: str, p: str) -> bool:

vue限制input内容为非空、数字,并且限制长度-爱代码爱编程

vue限制input内容为非空、数字,并且限制长度 html #prop 属性设置为需校验的字段名即可 #show-word-limit打开计数器 非必要 #maxlength="16" 限制输入内容长度最大为16位 <el-form-item label="测试" prop="code" class="el-col-md-11"> &l

shell 中酱紫的正则-爱代码爱编程

正则表达式分为: 正则表达式基本元字符正则表达式拓展元字符基本元字符 ^ 行首定位符 ^love $ 行尾定位符 love$ . 匹配单个字符

bugku web9-爱代码爱编程

flag In the variable ! <?php error_reporting(0); include "flag1.php"; highlight_file(__file__); if(isset($_GET['args'])){ $args = $_GET['args']; if(!preg_match("/^\w

如何在40亿个乱序的数中快速查找某个数值是否存在-爱代码爱编程

题目描述 给40亿个不重复的unsigned int的整数,没排过序的,然后再给一个数,如何快速判断这个数是否在那40亿个数当中? 分析与解法 海量数据处理往往会很有趣,有趣在什么地方呢? 空间,available的内存不够,需要反复交换内存 时间,速度太慢不行,毕竟那是海量数据 处理,数据是一次调用还是反复调用,因为针对时间和空间,通常来说,

数据结构笔记6:查找-爱代码爱编程

目录 查找基本概念: 线性结构 顺序查找 有序顺序查找 折半查找 分块查找 题目 树形结构 B树 m阶的B树性质: 操作 B+树 题目 散列表 构造 直接定址法 除留余数法 数字分析法 平⽅取中法 折叠法 处理冲突 开放定址法 拉链法 查找效率 题目 串 逻辑结构(定义) 存储结构(物理结构) 顺序

【LeetCode刷题】1550. 存在连续三个奇数的数组-爱代码爱编程

给你一个整数数组 arr,请你判断数组中是否存在连续三个元素都是奇数的情况:如果存在,请返回 true ;否则,返回 false 。 示例 1: 输入:arr = [2,6,4,1] 输出:false 解释:不存在连续三个元素都是奇数的情况。 示例 2: 输入:arr = [1,2,34,3,4,5,7,23,12] 输