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]

最新文章

  1. Mybatis批量删除
  2. python gevent 协程
  3. Netbeans 8.0.2 For PHP继续坑
  4. 动态平衡二叉搜索树的简易实现,Treap 树
  5. 【转】sql里面的split
  6. Sapi 添加语法的文章(转载)
  7. rails provide与content_for的区别
  8. 解决:Ubuntu12.04下使用ping命令返回ping:icmp open socket: Operation not permitted的解决
  9. 【IOS】关于CGTransform的几个动画
  10. Mac下Qt连接MySQL 驱动问题
  11. Android中网络流量控制(防火墙)——Iptables
  12. FPGA 设计流程,延迟,时间
  13. Linux中Mysql root用户看不到mysql库问题解决方式
  14. 第二十四节:Java语言基础-讲解数组的综合应用
  15. 数据结构(java版)学习笔记(四)——线性表之循环链表
  16. LAMP平台的搭建及应用
  17. NAT ------ 为什么手动设置NAT端口映射(转发)不成功,导致访问不了局域网服务器
  18. STL - 容器 - Map(二)
  19. 将Memcached作为服务自动启动
  20. [javaSE] 集合工具类(Collections-sort)

热门文章

  1. tf.nn.softmax_cross_entropy_with_logits的用法
  2. ubuntu 配置 django
  3. springMVC前后端分离开发模式下支持跨域请求
  4. 自己定义ImageView,实现点击之后算出点击的是身体的哪个部位
  5. vue2.0 flexible.js + rem 进行自适应开发
  6. odoo税金处理
  7. python(40)- 进程、线程、协程及IO模型
  8. EasyUI基础入门之Droppable(可投掷)
  9. mysql连接超时的问题
  10. 目标检测之显著区域检测---国外的一个图像显著区域检测代码及其效果图 saliency region detection