Python正则表达式中的量词符号与组问题小结
作者:Insane_Loafer 时间:2022-12-14 15:24:24
正则表达式中的符号
例子
|
是或的关系,只要存在就会被捕获匹配到的数据只按字符串顺序返回,而不是按照匹配规则返回
In [18]: data = 'insane@loafer.com'
In [19]: print(re.findall('insane|com|loafer', data))
['insane', 'loafer', 'com']
^
等同于 \A
In [20]: print(re.findall('^insane',data))
['insane']
In [21]: print(re.findall('^insane1',data))
[]
$
等同于 \Z
In [22]: print(re.findall('com$',data))
['com']
In [23]: print(re.findall('net$',data))
[]
*
匹配0次或多次
In [24]: print(re.findall('\w*',data))
['insane', '', 'loafer', '', 'com', '']
+
匹配1次或多次w+
匹配1次或多次数字或字母@
和.
属于0次范围,不会被匹配出来
In [25]: print(re.findall('\w+',data))
['insane', 'loafer', 'com']
{3}
表示对于匹配到的数据只获取3次
In [31]: data = 'insane@loaf.com'
In [32]: print(re.findall('\w{3}',data))
['ins', 'ane', 'loa', 'com']
In [33]: print(re.findall('[a-z]{3}',data))
['ins', 'ane', 'loa', 'com']
[a-zA-Z0-9]
基本上等同于\w
{M, N}
表示对于匹配到的数据只获取M~N次
In [34]: data = 'insane@loaf.com'
In [35]: print(re.findall('\w{1,4}',data))
['insa', 'ne', 'loaf', 'com']
反例:N
和 M
中间不能有空格
In [36]: print(re.findall('\w{1, 4}',data))
[]
[^...]
表示不匹配字符集中的字符
In [37]: data = 'insane@loaf.com'
In [38]: print(re.findall('[^insane]',data))
['@', 'l', 'o', 'f', '.', 'c', 'o', 'm']
组的概念
组的应用
In [42]: test = 'hello my name is insane'
In [43]: result = re.search('hello (.*) name is (.*)', test)
In [44]: result.groups()
Out[44]: ('my', 'insane')
In [45]: result.groups(1)
Out[45]: ('my', 'insane')
In [46]: result.group(1)
Out[46]: 'my'
In [47]: result.group(2)
Out[47]: 'insane'
贪婪与非贪婪 0次或多次属于贪婪模式
通过
?
组合变成非贪婪模式 实战
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time : 2021/8/28 22:13
# @Author : InsaneLoafer
# @File : re_test2.py
import re
def check_url(url):
"""
判断url是否合法
:param url:
:return:
"""
result = re.findall('[a-zA-Z]{4,5}://\w*\.*\w+\.\w+', url)
if len(result) != 0:
return True
else:
return False
def get_url(url):
"""
通过组获取url中的某一部分
:param url:
:return:
"""
result = re.findall('[https://|http://](\w*\.*\w+\.\w+)', url)
if len(result) != 0:
return result[0]
else:
return ''
def get_email(data):
# result = re.findall('[0-9a-zA-Z_]+@[0-9a-zA-Z]+\.[a-zA-Z]+', data)
result = re.findall('.+@.+\.[a-zA-Z]+', data)
return result
html = ('<div class="s-top-nav" style="display:none;">'
'</div><div class="s-center-box"></div>')
def get_html_data(data):
"""
获取style中的display:
使用非贪婪模式
"""
result = re.findall('style="(.*?)"', data)
return result
def get_all_data_html(data):
"""
获取html中所有等号后双引号内的字符
:param data:
:return:
"""
result = re.findall('="(.+?)"', data)
return result
if __name__ == '__main__':
result = check_url('https://www.baidu.com')
print(result)
result = get_url('https://www.baidu.com')
print(result, 'https')
result = get_url('http://www.baidu.com')
print(result, 'http')
result = get_email('insane@163.net')
print(result)
result = get_html_data(html)
print(result)
result = get_all_data_html(html)
print(result)
True
www.baidu.com https
www.baidu.com http
['insane@163.net']
['display:none;']
['s-top-nav', 'display:none;', 's-center-box']
Process finished with exit code 0
来源:https://blog.csdn.net/m0_48978908/article/details/119973638
标签:python,正则表达式,量词,组
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
设计的技术含量
2009-01-12 18:20:00
Python实现KNN(K-近邻)算法的示例代码
2023-09-25 15:56:18
![](https://img.aspxhome.com/file/2023/7/83957_0s.png)
MySql中表单输入数据出现中文乱码的解决方法
2024-01-22 20:34:13
Python简单定义与使用字典dict的方法示例
2023-03-10 03:22:48
![](https://img.aspxhome.com/file/2023/8/93668_0s.jpg)
教你利用python实现企业微信发送消息
2023-09-06 11:20:55
![](https://img.aspxhome.com/file/2023/2/105052_0s.png)
django如何实现视图重定向
2022-05-28 12:22:13
python 实现读取一个excel多个sheet表并合并的方法
2023-06-25 20:11:51
MySQL学习笔记5:修改表(alter table)
2024-01-23 23:01:16
MySQL中的字符串模式匹配
2010-03-09 16:30:00
利用phpmyadmin设置mysql的权限方法
2023-11-24 02:12:46
![](https://img.aspxhome.com/file/2023/4/65714_0s.png)
利用Python破解斗地主残局详解
2021-06-04 06:16:49
![](https://img.aspxhome.com/file/2023/2/92342_0s.jpg)
python Pandas之DataFrame索引及选取数据
2023-01-01 02:27:10
Python os.access()用法实例
2022-12-06 01:37:50
python中main函数(主函数)相关应用例子
2023-08-23 08:54:59
vue实现卡片翻转轮播展示
2024-04-27 16:08:16
![](https://img.aspxhome.com/file/2023/0/133000_0s.jpg)
CentOS7开启MySQL8主从备份、每日定时全量备份(推荐)
2024-01-15 20:31:18
关于AnacondaNavigator Jupyter Notebook更换Python内核的问题
2022-11-17 07:21:59
![](https://img.aspxhome.com/file/2023/7/121107_0s.jpg)
Python实现动态条形图绘制的示例代码
2021-09-25 03:05:17
![](https://img.aspxhome.com/file/2023/8/72098_0s.jpg)
Keras搭建Efficientdet目标检测平台的实现思路
2022-01-18 21:07:31
![](https://img.aspxhome.com/file/2023/6/131836_0s.jpg)
Python 字符串去除空格的五种方法
2023-01-15 08:23:56