re正则表达式公式讲解3
1.分组匹配 用()把需要分组的类型括起来,如下
import re
m = re.search("([a-z]+)([0-9]+)","alex123")
print(m.groups()) #用groups() 涉及到分组就用groups
# ('alex', '123')
2.”\A“ 表示从头开始匹配,和”^“ match 类似
“\Z” 匹配字符结尾,同$
“\d” 相当于数字0-9
import re
m = re.search("([a-z]+)([\d]+)","alex123")
print(m.groups())
('alex', '123')
“\D” 匹配非数字
import re
m = re.findall("\D","alex%467*123")
print(m)
# ['a', 'l', 'e', 'x', '%', '*']
“\w” 匹配(a-z A-Z 0-9)
import re
m = re.findall("\w","alex%467*123")
print(m)
# ['a', 'l', 'e', 'x', '4', '6', '7', '1', '2', '3']
“\W” 匹配非(a-z A-Z 0-9)
import re
m = re.findall("\W","alex%467*123")
print(m)
# ['%', '*']
“\s” 匹配空白字符
import re
m = re.findall("\s","ale\nx%46\t7*123")
print(m)
# ['\n', '\t']
“(?P<name>...)” 分组匹配 分组之间不需要加逗号,加了会报错。
import re
s = "321324199309187654"
m = re.search("(?P<convince>\d{3})""(?P<city>\d{3})""(?P<year>\d{4})""(?P<month>\d{4})",s)
print(m.groups()) # ('321', '324', '1993', '0918')
调用groupdict,生成字典。
import re
s = "321324199309187654"
m = re.search("(?P<convince>\d{3})""(?P<city>\d{3})""(?P<year>\d{4})""(?P<month>\d{4})",s)
print(m.groups()) print(dir(m))
print(m.groupdict()) # {'convince': '321', 'city': '324', 'year': '1993', 'month': '0918'}
re正则表达式公式讲解3的更多相关文章
- re正则表达式公式讲解6
标识符 re.I (re.IGNORECASE) 忽略大小写 import re s = "Max@123uyt146" print(re.search("m" ...
- re正则表达式公式讲解5
1.refullmatch() 完全匹配字符串则返回object,否则返回None import re s = "max@123uyt146" print(re.fullmatch ...
- re正则表达式公式讲解4
1.re,split() 字符串分离 import re s = "abc20tyu9iou16hij25" m = re.split("\d",s) #以& ...
- re正则表达式公式讲解1
常用的表达式一些规则 1.“.” 匹配出了\n之外的任意一个字符,包括特殊字符 有几个·就匹配几个字符. import re print(re.search("."," ...
- Jquery正则表达式公式.例子
1.非负整数 /^\d+$/ 2.正整数 /^[0-9]*[1-9][0-9]*$/ 3.非正整数 /^((-\d+)|(0+))$/ ...
- Javascript正则表达式详细讲解和示例,通俗易懂
正则表达式可以: •测试字符串的某个模式.例如,可以对一个输入字符串进行测试,看在该字符串是否存在一个电话号码模式或一个信用卡号码模式.这称为数据有效性验证 •替换文本.可以在文档中使用一个正则表达式 ...
- 前端js常用正则表达式实例讲解
本文内容整理自他人优秀的博客,非纯原创.仅借此学习和整理. 1.匹配用户名 规则描述: 长度4-6位: {4,16} 字母: [a-z] [A-Z] 数字: [0-9] 下划线: [_] 减号: [- ...
- Python爬虫(二)正则表达式
一.介绍 1.概念 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个"规则字符串",这个"规则字符串"用来 ...
- lotus 公式
函数 描述@UserName 返回用户名或服务器名.@Name([key]; name) 更改用户名的格式.关键字包含 [CN] 以从一个专有名字中解析出公共名,[Abbreviate] 缩写规范格式 ...
随机推荐
- Android源代码下载过程中无法下载repo的解决方法【转】
本文转载自:http://blog.csdn.net/shangyuan21/article/details/17618575 我们都知道下载Android源代码需要使用repo进行辅助下载,但是最进 ...
- MYSQL进阶学习笔记五:MySQL函数的创建!(视频序号:进阶_13)
知识点六:MySQL函数的创建(13) 内置函数: 自定义函数: 首先查看是否已经开启了创建函数的功能: SHOW VARIABLES LIKE ‘%fun%’; 如果变量的值是OFF,那么需要开启 ...
- iOS NSString拼接字符串
NSString* str_C; // 结果字符串NSString* str_A, str_B; //已存在的字符串,需要将str_A和str_B连接起来 //方法1 str_C = [NSStrin ...
- 【CAIOJ1177】 子串是否出现
[题目链接] 点击打开链接 [算法] KMP [代码] #include<bits/stdc++.h> using namespace std; #define MAXA 1000010 ...
- 【BZOJ 2818】 GCD
[题目链接] 点击打开链接 [算法] 线性筛出不大于N的所有素数,枚举gcd(x,y)(设为p),问题转化为求(x,y)=p的个数 设x=x'p, y=y'p,那么有(x,y)=1且 ...
- 【前端】CentOS 7 系列教程之一: 安装 node 最新版
转载请注明出处:http://www.cnblogs.com/shamoyuu/p/linux_1.html 此系列教程从零开始,安装node.mysql.git,nginx.并且设置git自动部署. ...
- Table View Programming Guide for iOS---(三)----Overview of the Table View API
Overview of the Table View API 表格视图API概述 The table view programming interface includes several UIKit ...
- Collection View Programming Guide for iOS---(一)----About iOS Collection Views
Next About iOS Collection Views 关于iOS Collection Views A collection view is a way to present an orde ...
- ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MVC 02. Web Host 的默认配置
视频地址: https://www.bilibili.com/video/av38392956/?p=2 语雀 https://www.yuque.com/yuejiangliu/dotnet/ixt ...
- 51nod 1347 【水】
#include<cstdio> #include <map> #include<iostream> #include<string.h> #inclu ...