regular
regular.py
import re # .
# 只能匹配一个字母,而不是2个或0个 # \
# 转义
# 'abc\\.com' r'abc\.com' # 字符集[]
# 匹配他所包括的任意字符
# '[pj]y' 匹配 'py'和 'jy'
# [a-z] [a-zA-Z0-9]
# 反转字符集 [^abc] 匹配除了a,b,c意外的字符
# 需要转义:^出现在开头,-和]出现在末尾 # 字符串开始^
# 字符串结尾$ # 选择符,子模式 |
# 'py|abc' 匹配py 和 abc
# 'p(aaa|bbb)' # 可选项和重复子模式
# 在子模式后加问号,变成可选项。问号表示子模式可以出现1次或不出现
# r'(http://)?(www\.)?python\.org'
# (pattern)* 重复0次,或多次
# (pattern)+ 重复1次或多次
# (pattern){m,n} 重复m~n次 # re 模块重要函数
# compile,search,match,split,findall,sub,escape P194
# re.search 寻找第一个匹配的子字符串,返回True,False
# re.match 从给定字符串的开头,匹配正则表达式
m = re.match(r'\[(.*)\]', 'www[aa]cc') # [不在开头,所以不能匹配
#[]加转义\,否则代表字符集
if m:
print(m.group(1))
m = re.match(r'.*\[(.*)\]', 'www[aa]cc')
if m:
print(m.group(1)) # re.findall 以列表,返回给定模式的所有匹配项
print(re.findall('[a-zA-Z]+', '"as...oo--as you it?"he said,fd.'))
print(re.findall(r'["\-.?]+', '"as...oo--as you it?"he said,fd.'))
print(re.findall(r'\[(.*)\]', 'www[aa]cc[,,]cc[mm0]11S')) # ['aa]cc[,,]cc[mm0']
print(re.findall(r'\[(.*?)\]', 'www[aa]cc[,,]cc[mm0]11S')) # 非贪婪,得到正确的 # re.escape 对字符串中所有可能被解释为正则运算符的字符,进行转义
print(re.escape('www.pp.com')) # www\.pp\.com # 匹配对象和 组
# group,start,end,span
# group 获取给定子模式(组)的匹配项
# start 返回给定组的匹配项的开始位置
# end 返回给定组的匹配项的开始位置
# span 返回一个组的开始和结束位置
m = re.match(r'www\.(.*)\..{3}', 'www.python.org')
print(m.group(1)) # python
print(m.start(1)) #
print(m.end(1)) #
print(m.span(1)) # (4,10)
m = re.match(r'www\.(.*)\..{3}', 'awww.python.org') # 不能匹配,match是在字符串开始处查找 # re.sub 使用给定的替换内容将匹配模式的子字符串替换掉
print(re.sub('{name}', 'abc', 'hello {name}...')) # hello abc...
# 作为替换的组号和函数
# re.sub 强大功能,在替换内容中以'\\n'形式出现的任何转义序列都会被模式中与组n匹配的字符串替换掉
# 例如要把'*something*' 用 '<em>something</em>' 替换掉
pat = r'\*([^\*]+)\*' # []字符集,^反转除了*以外的字符
print(re.sub(pat, r'<em>\1</em>', 'Hello,*world*!')) # Hello,<em>world</em>!
# re.sub高级,模板,见regular_test.py # 给正则表达式加注释
# 在re 函数中使用VERBOSE 标志
pat = re.compile(r'''
\* #comment1
( #start group
[^\*]+ #get group
) #end group
\* #end
''', re.VERBOSE)
regular_test.py
#运行 python regular_test.py magnux.txt template.txt
import fileinput,re
pat=re.compile(r'\[(.+?)\]') #非贪婪
scope={}
def replace(a):
code=a.group(1)
try:
return str(eval(code,scope))
except SyntaxError:
exec(code,scope)
return '' lines=[]
for line in fileinput.input():
lines.append(line) text=''.join(lines)
print(text)
print(pat.sub(replace,text)) #因为magnus.txt中是赋值,所以replace没有返回str 而是exec,
#所以没有打印
magnus.txt
[name='tttttt']
[email = 'aaaa.com']
[langue = 'python']
template.txt
[import time]
Dear [name],
the [langue] asdf asdf asdf
[email]
[time.asctime()] [x=1]
[y=2]
sum of [x] and [y] is [x+y]
regular的更多相关文章
- [LeetCode] Regular Expression Matching 正则表达式匹配
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- MongoVUE1.6.9破解启动提示System.ArgumentException: 字体“Courier New”不支持样式“Regular”
用MongoVUE,发现报错,报错信息如下: System.ArgumentException: 字体"Courier New"不支持样式"Regular". ...
- myeclipse中导入js报如下错误Syntax error on token "Invalid Regular Expression Options", no accurate correc
今天在使用bootstrap的时候引入的js文件出现错误Syntax error on token "Invalid Regular Expression Options", no ...
- [LeetCode] 10. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...
- No.010:Regular Expression Matching
问题: Implement regular expression matching with support for '.' and '*'.'.' Matches any single charac ...
- scp报错:not a regular file,解决方法:加参数 -r
命令:scp -P1234 /data/aa root@192.0.0..0:/data 文件结构:/data/aa/yearmonth=2015-09 报错:not a regular fi ...
- Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- glyphicon halflings regular ttf 报错
一个web项目 用了bootstrap chrome开f12报错提示glyphicon halflings regular ttf找不到 为什么找不到,肯定又是path出了问题 找到bootstrap ...
- PCRE Perl Compatible Regular Expressions Learning
catalog . PCRE Introduction . pcre2api . pcre2jit . PCRE Programing 1. PCRE Introduction The PCRE li ...
- cf#306D. Regular Bridge(图论,构图)
D. Regular Bridge time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
随机推荐
- 转:Json 语法 格式
转自: http://www.cnblogs.com/chencidi/archive/2011/03/24/1993450.html 评注: json 官网如下: http://json.org/j ...
- PS 如何使用抽出滤镜抠人物的头发丝等细节
1.打开图片,复制背景,关闭背景眼睛.单击 滤镜 -抽出, 我们要学会观察图片,先来看下面这张图: 这张图片色彩虽然不算丰富,但也不是纯色背景,甚至有些许的零乱,但人物的主题却被黑色长发包围, 我们只 ...
- sql分级汇总
--測试数据 create table tb([DB-ID] varchar(10),ENTITY varchar(10),DATE varchar(10),[CUST-NO] int,AMOUNT ...
- html嵌套规则
本人半路出家的 今天学习js的时候写了一个a嵌套a标签结果js报错 一直找不到原因 专门找了一下html嵌套规则看了一下 1.块级元素 一般用来搭建网站架构.布局.承载内容……它包括以下这些标签: ...
- H5实现多图片预览上传,可点击可拖拽控件介绍
版权声明:欢迎转载,请注明出处:http://blog.csdn.net/weixin_36380516 在做图片上传时发现一个蛮好用的控件,支持多张图片同时上传,可以点击选择图片,也可以将图片拖拽到 ...
- 【转载】Web Service和WCF的到底有什么区别
[1]Web Service:严格来说是行业标准,也就是Web Service 规范,也称作WS-*规范,既不是框架,也不是技术. 它有一套完成的规范体系标准,而且在持续不断的更新完善中. 它使用XM ...
- DataTable和DataRow利用反射直接转换为Model对象的扩展方法类
DataTable和DataRow利用反射直接转换为Model对象的扩展方法类 /// <summary> /// 类 说 明:给DataTable和DataRow扩展方法,直接转换为 ...
- 做QA的日子——iOS測试入门(四)
坦言,做QA的这半年我没有成长,就算有成长也非常少,我非常难过.和身边的人讲事实上并没有谁能真正理解自己的难过,事实上还是自己不够努力.对自己不够狠,以前认为自己不够幸运,想有一个更好的指路人,事实上 ...
- DB 【ACID】
http://blog.csdn.net/shuaihj/article/details/14163713 http://blog.csdn.net/dief913975849/article/det ...
- 在Linux的Eclipse下搭建Android环境
http://blog.csdn.net/lyonte/article/details/6407242 一.Java环境安装配置详见<在Linux下搭建Java环境>http://blog ...