#!/usr/bin/env python
# coding: utf-8
#copyRight by heibanke
import re
text = 'c++ python2 python3 perl ruby lua java javascript php4 php5 c'
#match,search,findall,split,sub
print re.match(r'java', text) #从头找,开头是c++,所以是none
m = re.search(r'java', text) #任意位置找
print m.start(),m.end() #返回起始位置和结束位置
#re.match(r'c++',text) #错误,+是一个正则表达式的符号
print re.match(r'c\+\+',text) #转义后正确
print re.findall(r'python',text) #找到所有含跑python的
print re.split(r' perl ',text) #从perl切开text,返回列表
print re.sub(r'ruby','fortran',text) #ruby替换成fortran
# ^ start
# $ end
# . except \n
print re.findall(r'^c..',text) #^从头开始匹配,.是可以匹配除回车外的所有字符,这里可以匹配到c++,一个.的话匹配到c+
print re.findall(r'c+',text) #+是1到正无穷个c,如c,cc,ccc,……
print re.findall(r'c\++',text) #匹配c+,c++,c+++,……
print re.findall(r'c$',text) #从结尾匹配
# + 1-inf
# * 0-inf
# ? 0-1,
# [] or 或
# {} repeat
# [^] not
print re.findall(r'p+',text)
print re.findall(r'p[a-zA-Z]+',text) #{1,},不可以匹配单独的p
print re.findall(r'p[a-zA-Z]*',text) #{0,},可以匹配p
print re.findall(r'p[a-zA-Z]?',text) #{0,1},只能0或1个
print re.findall(r'p[a-zA-Z0-9]{3,}',text) #{最小重复次数,最大重复字数}
print re.findall(r'c[a-zA-Z]*',text) #{,inf}
print re.findall(r'c[^a-zA-Z]*',text) #{,inf} ^在中括号里是非的意思
# | or
print re.findall(r'[pj][a-zA-Z]+',text) #{,inf}
#['python', 'python', 'perl', 'java', 'javascript', 'php', 'php']
# |重写上面的pattern
print re.findall('p[a-zA-Z]+|j[a-zA-Z]+',text)
print re.findall(r'p[^0-9]+|j[a-zA-Z]+',text) #空格也可以,所以将“perl ruby lua java javascript php”识别为一个 ???为什么php也识别进去了
#不明白!!!问老师吧
#识别到非数字啊傻
print re.findall(r'p[^0-9 ]+|j[a-zA-Z]+',text)
# \w [a-zA-Z0-9_], \W 大写是非的意思
# \d [0-9], \D
# \s [ \t\n\r\f\v], \S
print re.findall(r'p\w+',text)
print re.findall(r'p\w+\d',text)
print re.findall(r'p\w+[0-9]',text)
print re.findall(r'p\w{5,9}',text)
# \b word boundary 边界
# \B not \b
# \A input start, ^
# \Z input end, $
print re.findall(r'\bp[^0-9]',text) #识别左边界的
print re.findall(r'p[^0-9]\b',text) #右边界的px
print re.findall(r'p[^0-9]\b',text)
# *? 0~inf non-greedy
# +? 1~inf non-greedy
print re.findall(r'p[a-z]*',text) #尽可能多字母
print re.findall(r'p[a-z]*?',text) #尽可能少字母,·[p0个]
print re.findall(r'p[a-z]+\b',text)
print re.findall(r'p[a-z]+?\b',text) #尽可能少项,[p1个]
# () group
# (?P<name>pattern)
a=re.search(r'(p[a-zA-Z]+)([0-9])','python2',re.X) # 分组python和2
print a.group(1), a.group(2)
a=re.search(r'(?P<name>p[a-zA-Z]+)(?P<version>[0-9])','python2')
print a.group('name'), a.group('version')
print a.groupdict()
pattern = re.compile(r'(?P<name>p[a-zA-Z]+)(?P<version>[0-9])')
results = pattern.search('python2')
print results.groupdict()
results = pattern.search('python3')
print results.groupdict()
results = pattern.search('php4')
print results.groupdict()
#########################################
for t in text.split(' '):
results = pattern.search(t)
if results:
print results.groupdict()
###########################################
a = re.compile(r"""\d + # 整数部分
\. # 小数点
\d * # 小数部分
""", re.X)
b = re.compile(r"\d+\.\d*")
结果
None
34 38
['python', 'python']
['c++ python2 python3', 'ruby lua java javascript php4 php5 c']
c++ python2 python3 perl fortran lua java javascript php4 php5 c
['c++']
['c', 'c', 'c']
['c++']
['c']
['p', 'p', 'p', 'p', 'p', 'p', 'p', 'p']
['python', 'python', 'perl', 'pt', 'php', 'php']
['python', 'python', 'perl', 'pt', 'php', 'php']
['py', 'py', 'pe', 'pt', 'ph', 'p', 'ph', 'p']
['python2', 'python3', 'perl', 'php4', 'php5']
['c', 'cript', 'c']
['c++ ', 'c', 'c']
['python', 'python', 'perl', 'java', 'javascript', 'php', 'php']
['python', 'python', 'perl', 'java', 'javascript', 'php', 'php']
['python', 'python', 'perl ruby lua java javascript php', 'php']
['python', 'python', 'perl', 'java', 'javascript', 'php', 'php']
['python2', 'python3', 'perl', 'pt', 'php4', 'php5']
['python2', 'python3', 'php4', 'php5']
['python2', 'python3', 'php4', 'php5']
['python2', 'python3']
['py', 'py', 'pe', 'ph', 'ph']
['pt']
['pt']
['python', 'python', 'perl', 'pt', 'php', 'php']
['p', 'p', 'p', 'p', 'p', 'p', 'p', 'p']
['perl', 'pt']
['perl', 'pt']
python 2
python 2
{'version': '2', 'name': 'python'}
{'version': '2', 'name': 'python'}
{'version': '3', 'name': 'python'}
{'version': '4', 'name': 'php'}
{'version': '2', 'name': 'python'}
{'version': '3', 'name': 'python'}
{'version': '4', 'name': 'php'}
{'version': '5', 'name': 'php'}
有关分组
#coding:utf-8
import re
text = '1abc23456789'
print re.findall(r'([0-9a-z]+)2([0-9]+)6([0-9]+)', text)
结果
[('1abc', '345', '7')]
这里的2、6代表以这个为界限查找