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. 第四讲_图像识别之图像分类Image Classification

    第四讲_图像识别之图像分类Image Classification 目录 图片分类 性能指标:top1,top5 ILSVRC:每种任务数据集不一样 imageNet:根据WorldNet组织的图片集 ...

  2. 【转】Ubuntu下出现Mysql error(2002)的解决方法

    过了一阵子后,为了写分布式作业,重新使用Mysql时,发现虽然启动成功了,但是连接的时候去出现如下错误ERROR 2002 (HY000): Can't connect to local MySQL ...

  3. web图片转换小工具制作

    HTML <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...

  4. sonar + ieda实现提交代码前代码校验

    代码风格不同一直是一件停头疼的事情,因为不同的工作经验,工作经历,每个人的代码风格不尽相同,造成一些代码在后期的维护当中难以维护, 查阅一些资料之后发现 idea + sonar 的方式比较适合我,实 ...

  5. Android SQLite性能分析

    作为Android预置的数据库模块,对SQLite的深入理解是很有必要的,能够从中找到一些优化的方向. 这里对SQLite的性能和内存进行了一些測试分析.对照了不同操作的运行性能和内存占用的情况,粗略 ...

  6. iOS应用数据存储的经常使用方式

    ios程序中数据数据存储有下列5种方式 XML属性列表(plist)归档 Preference(偏好设置) NSKeyedArchiver归档(NSCoding) SQLite3 Core Data ...

  7. Flash文字效果

    flash中增加文本.使用了消除锯齿:可读性消除锯齿.发现不嵌入字体的无法动态改动里面的文字,但嵌入字体的话会造成swf文件过大. 终于还是选择了使用设备字体,并选择了黑体.出了一个问题.文字没有加粗 ...

  8. 文件I/O相关函数

    open()和openat()函数: #include <fcntl.h> // 成功返回文件描述符,出错返回-1 int open(const char *path, int oflag ...

  9. kubernetes之初始容器(init container)

    系列目录 理解初始容器 一个pod里可以运行多个容器,它也可以运行一个或者多个初始容器,初始容器先于应用容器运行,除了以下两点外,初始容器和普通容器没有什么两样: 它们总是run to complet ...

  10. LeetCode(88)题解-- Merge Sorted Array

    https://leetcode.com/problems/merge-sorted-array/ 题目: Given two sorted integer arrays nums1 and nums ...