import re
origin= "hello alex bcd alex 1ge alex acd 19"
r=re.match("(?P<n1>h)(?P<n2>\w+)",origin)
print(r.group())#获取匹配所有结果
print(r.groups())#获取模型中匹配的分组结果
print(r.groupdict())##获取模型中匹配的分组结果r=re.match("(?P<n1>h)(\w+)",origin)
'''
hello
('h', 'ello')
{'n2': 'ello', 'n1': 'h'}
'''

groups()就是显示选组(),而groupdict就是在分组加入一个关键字key

a = "alex"
n = re.findall('(\w)(\w)(\w)(\w)',a)
print(n)
n = re.findall('(\w){4}',a)
print(n)
n = re.findall("",a) #匹配空['', '', '', '', '']会比字符串多一个,就是最后再匹配一次
print(n) str1 = "1abc2abcq3abc4abc"
t = re.findall('(\dabc)*',str1)#结是为什么是['2abc', '', '4abc', '']
#这个是因为匹配*为贪婪模式1abc2abc但括号取值取最后一个,到q是*可以为空,空匹配到,接着3abc4abc取后面4abc,完后还有一个空匹配
print(t)
运行结果

[('a', 'l', 'e', 'x')]
['x']
['', '', '', '', '']
['2abc', '', '4abc', '']

 import re
def f(n):
while True:
if re.search('\d+\.?\d*[*/]\d+\.?\d*',n):#匹配小数点
ret=re.search('\d+\.?\d*[*/]\d+\.?\d*',n).group()
# print(ret)
t=re.split('([*/])',ret)
# print(t)
if t[1] == '*':
sum = float(t[0])*float(t[2])
else:
sum = float(t[0])/float(t[2])
n1 = n.replace(ret,str(sum))
# print(n1)
n = n1
t.clear()
else:
break
while True:
if re.search('\d+\.?\d*[+-]\d+\.?\d*',n):
ret=re.search('\d+\.?\d*[+-]\d+\.?\d*',n).group()
# print(ret)
t=re.split('([+-])',ret)
# print(t)
if t[1] == '+':
sum = float(t[0])+float(t[2])
else:
sum = float(t[0])-float(t[2])
n1 = n.replace(ret,str(sum))
# print(n1)
n = n1
t.clear()
else:
break
return n
n1 = '1+0*(3*4+(1+3))+23-2.51*0'
print(n1)
flag = True
while flag:
t1=re.split('\(([^()]+)\)',n1,1)#split分割出来去掉()
if len(t1) == 3 :
str1 = f(t1[1])#调用加减乘除函数
t1[1]=str(str1)
n1="".join(t1)
# print(n1)
else:
flag = False
n2=f(n1)
print(n2)

 n='1+2*3+3*4'
n1, count = re.subn('\*','/',n)
print(n1,count)
运行结果
1+2/3+3/4 2

												

python正则表达式补充的更多相关文章

  1. Python 正则表达式 (python网络爬虫)

    昨天 2018 年 01 月 31 日,农历腊月十五日.20:00 左右,152 年一遇的月全食.血月.蓝月将今晚呈现空中,虽然没有看到蓝月亮,血月.月全食也是勉强可以了,还是可以想像一下一瓶蓝月亮洗 ...

  2. python正则表达式-re模块

    目录: 一.正则函数 二.re模块调用 三.贪婪模式 四.分组 五.正则表达式修饰符 六.正则表达式模式 七.常见的正则表达式 导读: 想要使用python的正则表达式功能就需要调用re模块,re模块 ...

  3. 【python之路39】Python 正则表达式

    Python 正则表达式 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配. Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式. r ...

  4. Python 正则表达式入门(中级篇)

    Python 正则表达式入门(中级篇) 初级篇链接:http://www.cnblogs.com/chuxiuhong/p/5885073.html 上一篇我们说在这一篇里,我们会介绍子表达式,向前向 ...

  5. Python正则表达式中的re.S

    title: Python正则表达式中的re.S date: 2014-12-21 09:55:54 categories: [Python] tags: [正则表达式,python] --- 在Py ...

  6. Python 正则表达式入门(初级篇)

    Python 正则表达式入门(初级篇) 本文主要为没有使用正则表达式经验的新手入门所写. 转载请写明出处 引子 首先说 正则表达式是什么? 正则表达式,又称正规表示式.正规表示法.正规表达式.规则表达 ...

  7. python正则表达式re

    Python正则表达式: re 正则表达式的元字符有. ^ $ * ? { [ ] | ( ).表示任意字符[]用来匹配一个指定的字符类别,所谓的字符类别就是你想匹配的一个字符集,对于字符集中的字符可 ...

  8. Python正则表达式详解

    我用双手成就你的梦想 python正则表达式 ^ 匹配开始 $ 匹配行尾 . 匹配出换行符以外的任何单个字符,使用-m选项允许其匹配换行符也是如此 [...] 匹配括号内任何当个字符(也有或的意思) ...

  9. 比较详细Python正则表达式操作指南(re使用)

    比较详细Python正则表达式操作指南(re使用) Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式.Python 1.5之前版本则是通过 regex 模块提供 E ...

随机推荐

  1. 使用特性将数据库返回的datatable转换成对象列表

    public class ColumnMapAttribute : Attribute { private readonly string _name; public ColumnMapAttribu ...

  2. EDK II之Secure Boot简述

    密钥对:公钥分发,私钥自留.常见的公钥格式:cer/der,常见的私钥格式:pfx. BIOS中Secure Boot的原理:把公钥包在code里面,当使用gBS->LoadImage()去加载 ...

  3. composer安装doctrine/dbal

    composer安装doctrine/dbal composer安装doctrine/dbal,安装不成功,使用的安装命令为官方提供命令“composer require doctrine/dbal” ...

  4. 【题解】Luogu P4121 [WC2005]双面棋盘

    原题传送门 这道题肥肠毒瘤qwqwq,我被卡了qwqwq 这题的正解好像是线段树+并查集,但由于我人丑常数大被卡成了70 #include <bits/stdc++.h> #define ...

  5. 顺手写一下HTTP协议

    本文目录 一 什么是HTTP协议 二 Http的特点 三 Http报文 回到目录 一 什么是HTTP协议 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写 ...

  6. Swift-Extensions

    日常开发中,frame 是我们经常用到的,但是 UIKit 不允许我们直接设置 frame.origin.x frame.origin.y frame.size.wight frame.size.he ...

  7. 配置IPMI

    服务器电源管理 查看服务器电源状态 ipmitool chassis power status 关闭服务器电源 ipmitool chassis power off 打开服务器电源 ipmitool ...

  8. 洛谷 P1027 【Car的旅行路线】

    题目描述 又到暑假了,住在城市A的Car想和朋友一起去城市B旅游.她知道每个城市都有四个飞机场,分别位于一个矩形的四个顶点上,同一个城市中两个机场之间有一条笔直的高速铁路,第i个城市中高速铁路的单位里 ...

  9. IntelliJ IDEA 和 Pycharm 破解

    关键网址:http://idea.lanyus.com/ 步骤: 1. 在http://idea.lanyus.com/上下载:JetbrainsCrack-2.9-release-enc.jar . ...

  10. CART回归树

    决策树算法原理(ID3,C4.5) 决策树算法原理(CART分类树) 决策树的剪枝 CART回归树模型表达式: 其中,数据空间被划分为R1~Rm单元,每个单元有一个固定的输出值Cm.这样可以计算模型输 ...