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的更多相关文章

  1. [LeetCode] Regular Expression Matching 正则表达式匹配

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  2. MongoVUE1.6.9破解启动提示System.ArgumentException: 字体“Courier New”不支持样式“Regular”

    用MongoVUE,发现报错,报错信息如下: System.ArgumentException: 字体"Courier New"不支持样式"Regular". ...

  3. myeclipse中导入js报如下错误Syntax error on token "Invalid Regular Expression Options", no accurate correc

    今天在使用bootstrap的时候引入的js文件出现错误Syntax error on token "Invalid Regular Expression Options", no ...

  4. [LeetCode] 10. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...

  5. No.010:Regular Expression Matching

    问题: Implement regular expression matching with support for '.' and '*'.'.' Matches any single charac ...

  6. 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 ...

  7. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  8. glyphicon halflings regular ttf 报错

    一个web项目 用了bootstrap chrome开f12报错提示glyphicon halflings regular ttf找不到 为什么找不到,肯定又是path出了问题 找到bootstrap ...

  9. PCRE Perl Compatible Regular Expressions Learning

    catalog . PCRE Introduction . pcre2api . pcre2jit . PCRE Programing 1. PCRE Introduction The PCRE li ...

  10. cf#306D. Regular Bridge(图论,构图)

    D. Regular Bridge time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

随机推荐

  1. SilverLight: 数据绑定(1)-绑定到数据对象

    ylbtech-SilverLight-DataBinding: Binding to Data Objects(绑定到数据对象) 1.A, Building  a Data Object(创建一个数 ...

  2. ShadowMap渲染阴影方法及问题 【转】

    ShadowMap基于的原理:SM算法是一个2-pass绘制算法,第一pass从光源视点绘制场景,生成SM纹理,第2pass从视点视图按常规方法绘制场景 从光源的位置观察场景,这时候我们看不到的地方就 ...

  3. jQeury入门:遍历

    一旦用jQuery创建一个初始的包装集.你就能深度遍历刚刚选择的包装集. 遍历能够被分为三个基础元件:父级.子级,同级.jQuery为全部这些元件提供丰富易用的方法.注意每个方法都能轻易的传递给字符串 ...

  4. 广告制胜无它,顺应人性尔——leo鉴书63

    近期看了几本怎样写文案的书.对广告有了些兴趣.查了下相关销量排行,位置比較高的是本叫<科学的广告+我的广告生涯>的书,是同一作者(Claude C. Hopkins)两本书的合集.前者是他 ...

  5. 串匹配算法之BM算法

    参考资料: http://blog.csdn.net/eric491179912/article/details/6210009   http://blog.163.com/pengfeicui@ye ...

  6. JavaScript闭包其一:闭包概论 函数式编程中一些基本定义

    http://www.nowamagic.net/librarys/veda/detail/1707前面介绍了作用域链和变量对象,现在再讲闭包就容易理解了.闭包其实大家都已经谈烂了.尽管如此,这里还是 ...

  7. soapUI学习笔记--用例字段参数化

    字段参数化的简单操作 1.把Request新增一个TestCase 增加TestCase,下方会出现: 2.案例中,请求参数只有一个.先运行下请求,可以运行成功(保证接口是通的) 3.添加参数.见图中 ...

  8. 使用 Kingfisher 处理网络图片的读取与缓存

    Kingfisher 是一个读取网络图片和处理本地缓存的开源库,由 onevcat 开发.提到图片缓存库,那么熟悉 Objective-C 开发的同学,可能会想起 SDWebImage. 没错,Kin ...

  9. 编程算法 - 多重部分和问题 代码(C)

    多重部分和问题 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 有n种不同大小的数字a, 每种各m个. 推断能否够从这些数字之中选出若干使它们的 ...

  10. 服务管理-DHCP、NTP、SSH

    DHCP协议的作用及原理 1.寻找server 当DHCP客户端第一次登陆网络的时候,也就是客户发现本机上没有任何ip资料设定,他会向网路发送一个dhcpdiscover封包.因为客户端还不知道自己属 ...