python--正则

正则表达式是一个单独的部分,无论什么语言,都需要正则表达式。
正则表达式:正则表达式是用了匹配或提取字符串的。正则表达式的语法无论在哪里使用都是相同的。先看一个实例:
判断用户提交的邮箱格式是否正确

import re
yx = '13900003333@163.com'
yxx = re.findall(r'[a-zA-Z0-9]+@[a-zA-Z0-9]\.com$',yx)
print(yxx)
返回:13900003333@163.com
yx = 'abcdefg@163.com'
yxx = re.findall(r'[a-zA-Z0-9]+@[a-zA-Z0-9]\.com$',yx)
print(yxx)
返回:abcdefg@163.com
yx = '13900003333@163.cn'
yxx = re.findall(r'[a-zA-Z0-9]+@[a-zA-Z0-9]\.com$',yx)
print(yxx)
返回:[] #匹配失败
yx = '13900003333163.com'
yxx = re.findall(r'[a-zA-Z0-9]+@[a-zA-Z0-9]\.com$',yx)
print(yxx)
返回:[] #匹配失败

在上例中,r'[a-zA-Z0-9]+@[a-zA-Z0-9].com$' ‘ r’ 是表示python的防转义,而 \ 是正则表达式的防转义,这是两个不同的转义

re模块

在python中要使用正则,必须要导入re模块。

import re
re 中的常用方法

1.findall 方法:在字符串中找到正则表达式所匹配的所有子串,并返回一个列表,如果没有找到匹配的,则返回空列表.

re.findall(pattern,string)  #(匹配的语法规则,字符串)
import re
a = 'hello world pthon hello world pthon hello world pthon'
b = re.findall('llo',a)
print(b)
返回:['llo', 'llo', 'llo']
b = findall('123',a)
print(b)
返回:[] #字符串中没有数字,所以匹配失败,返回空 []

2.match方法:re.match 尝试从字符串的起始位置匹配一个模式,匹配成功 返回的是一个匹配对象(这个对象包含了我们匹配的信息),如果不是起始位置匹配成功的话,match()返回的是空,
注意:match只能匹配到一个

import re
a = 'hello world pthon hello world pthon hello world pthon'
b = re.match('llo',a)
print(b)
返回:None
print(b.group())
返回:hel #group() 可以查看匹配的内容

a = 'hello world pthon hello world pthon hello world pthon'
b = re.match('hel',a)
print(b)
返回:<re.Match object; span=(0, 3), match='hel'> #span 是字符所在的x下标取值区间

3.search方法:re.search 扫描整个字符串,匹配成功 返回的是一个匹配对象(这个对象包含了我们匹配的信息)
注意:search也只能匹配到一个,找到符合规则的就返回,不会一直往后找

import re
a = 'hello world pthon hello world pthon hello world pthon'
b = re.search('llo',a)
print(b)
返回:<re.Match object; span=(2, 5), match='llo'>

a = 'hello world pthon hello world pthon hello world pthon'
b = re.search('234',a)
print(b)
返回:None
#group() 可以查看匹配的内容,同match相同。

re.match与re.search的区别:
re.match只匹配字符串的开始位置找,如果字符串开始不符合正则表达式,则匹配失败,
re.search:匹配整个字符串,如果一直找不到,返回是空的,没有结果
4.sub方法:re.sub 是替换字符串。

import re
a = 'hello world hello pythpn hello java'
b = re.sub('hello','hi',a)  #('原字符','替换的字符',string)
print(b)
返回:hi world hi pythpn hi java

在正则表达式中,如果加了()就会只提取()里面的内容。

import re
a = '''
    <div id="redGame"><em></em><span>
       <a target="_blank" href="http://app.tanwan.com/htmlcode/9093.html"></a>
       <a target="_blank" href="https://app.tanwan.com/htmlcode/9094.html"></a>
       <a target="_blank" href="https://app.tanwan.com/htmlcode/9095.html"></a>
       <a target="_blank" href="https://app.tanwan.com/htmlcode/9096.html"></a>
       <a target="_blank" href="https://app.tanwan.com/htmlcode/9097.html"></a>
       <a target="_blank" href="http://app.tanwan.com/htmlcode/9098.html"></a>
       <a target="_blank" href="https://app.tanwan.com/htmlcode/9099.html"></a>
       <a target="_blank" href="https://app.tanwan.com/htmlcode/9100.html"></a></span>
    </div>
'''

b = re.findall('href="(.*?)"',a)  #只会提取()内的内容,href当作一个标签
for i in b:
    print(i)
返回:
http://app.tanwan.com/htmlcode/9093.html
https://app.tanwan.com/htmlcode/9094.html
https://app.tanwan.com/htmlcode/9095.html
https://app.tanwan.com/htmlcode/9096.html
https://app.tanwan.com/htmlcode/9097.html
http://app.tanwan.com/htmlcode/9098.html
https://app.tanwan.com/htmlcode/9099.html
https://app.tanwan.com/htmlcode/9100.html

元字符

本身具有特殊意义的字符叫做元字符,比如:* ,$ ,[] ,. ,等。

单字符匹配
图1

代表数量的元字符
图2

表示边界的元字符(锚点元字符)
图3

分组匹配
图4
管道 |
import re
a = 'hello world pthon hello world pthon hello world pthon'
b = re.findall('hello|python',a)
print(b)
返回:['hello', 'pthon', 'hello', 'pthon', 'hello', 'pthon']

字符组:
a = 'hello world pthon hello world pthon hello world pthon'
b = re.findall('[he]',a)  #整个中括号只代表一个字符
print(b)
返回:['h', 'e', 'h', 'h', 'e', 'h', 'h', 'e', 'h']

a = 'abbj abbc abbd'
b = re.findall('abb[abd]',a)
print(b)
返回:['abbd']

比较[] 与 []{}

import re
a = '1209809wiahiwqdi821e01'
b = re.findall('[a-zA-Z0-9]{5}',a)
print(b)
返回:['12098', '09wia', 'hiwqd', 'i821e']

a = '1209809wiahiwqdi821e01'
b = re.findall('[a-zA-Z0-9]',a)
print(b)
返回:['1', '2', '0', '9', '8', '0', '9', 'w', 'i', 'a', 'h', 'i', 'w', 'q', 'd', 'i', '8', '2', '1', 'e', '0', '1']

反向字符类(取反): ^

import re 
a = '1209809 alsj rpokgtp'
b = re.findall('[^a-zA-Z0-9]',a)  # ^表示除了[]里面的规则外的其他字符,^写在[]前面表示从头取,写在规则内,表示取反。
print(b)
返回:[' ', ' ']

转义元字符: \ 正则规则中 \ 可以转义其他的特殊元字符.

\*  ,   \.  ,   \+  ,   \?   等

贪婪与非贪婪

正则默认都是用贪婪模式去匹配数据的,就是尽可能多的匹配符合要求的数据,在非贪婪模式下,始终找最短匹配

import re
a = 'abbbbbbbbc'
b = re.findall('ab*',a)  #('ab*c',a)#如果*后面有 c ,则必须要满足后面c的匹配。
b1 = re.findall('ab*?',a)  #前面的元字符(*)是正则规则,后面的?是非贪婪模式
print(b)
print(b1)
返回:
['abbbbbbbb']  #贪婪模式是尽可能取除多的字符串。* 可以取出所有。
['a']  #非贪婪模式是尽可能少取字符串,* 可以是0.

一个元字符表示贪婪模式,只有在后面加?可以变为非贪婪模式。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,711评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,932评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,770评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,799评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,697评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,069评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,535评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,200评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,353评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,290评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,331评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,020评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,610评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,694评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,927评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,330评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,904评论 2 341

推荐阅读更多精彩内容

  • re模块手册 本模块提供了和Perl里的正则表达式类似的功能,不关是正则表达式本身还是被搜索的字符串,都可以...
    喜欢吃栗子阅读 3,967评论 0 13
  • 搞懂Python 正则表达式用法 Python 正则表达式 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一...
    厦热阅读 1,560评论 0 2
  • #首先,python中的正则表达式大致分为以下几部分: 元字符 模式 函数 re 内置对象用法 分组用法 环视用法...
    mapuboy阅读 1,589评论 0 51
  • 本文介绍了Python对于正则表达式的支持,包括正则表达式基础以及Python正则表达式标准库的完整介绍及使用示例...
    Python程序媛阅读 1,337评论 0 22
  • 一般情况下,在某个社交聚会上,如果听说在场的某个人会看手相,会使用塔罗牌占卜,或者会看星盘,那么这个人很...
    彭薇阅读 237评论 0 1