标签:erro select res uil ada end 集合 而且 号码
import re
data = ‘bat sad bit ad but d fa hat adfad hit ad da d hut aa‘
patt = ‘[bh][uia]t‘
m = re.findall(patt,data)
print(m)
data = ‘adfass323f sadfa aadfa‘
patt = ‘\w+\s\w+‘
m = re.match(patt, data)
m.group()
data = ‘adfaf aa, dadada‘
patt = ‘\w+,\s\w+‘
m = re.search(patt, data)
m.group()
data = ‘adaas1212dfdfsadfaaf_‘
patt = ‘[_A-Za-z]+[_\w]+‘ #有效Python标识符:以下划线和字母开头的非空字符,
m = re.match(patt, data)
m.group()
data = ‘3120 De la Cruz Bou levard‘
patt = r‘\d+.+‘ #以数字开头的任何字符串
m = re.match(patt,data)
m.group()
data = ‘http://www.y.as.sadf.a121.2asasdoo.cc‘
patt = r‘http://(www\.[\w|\.]+\.(com|cn|org|net|cc))‘
m = re.match(patt,data)
m.group(1)
data = ‘-123141234‘
patt = ‘[-|+]?\d+$‘
m = re.match(patt, data)
m.group()
data = ‘123141234L‘
patt = ‘[-|+]?\d+[lL]‘
m = re.match(patt, data)
m.group()
data = ‘-12310.09‘
patt = ‘[-|+]?\d+\.\d*‘
m = re.match(patt, data)
m.group()
data = ‘121+989i‘
patt = r‘\d+(\+\d*i)‘ #简单的实部和虚部都是整数
m = re.match(patt, data)
m.group()
data = ‘y.1-a-9.a@t.w-0.9-3-c.com‘
patt = r‘[a-zA-Z]+([-\.]?\w+)+@((\w+-?\w*)+\.)+\w{2,3}‘ #6~18个字符,可使用字母、数字、下划线,需以字母开头
m = re.match(patt, data)
m.group()
data = ‘http://www.ba-i.du.com.cn http://ww.qq.com‘
patt = r‘http://((\w+[-\.])*\w+)‘
m = re.match(patt, data).group()
n = re.findall(patt, data)
print(n)
该对象将表示为一个 Pythonic类型的字符串。
>>> type(0)
<type ‘int‘>
>>> type(.34)
<type ‘float‘>
>>> type(dir)
<type ‘builtin_function_or_method‘>
创建一个能够从字符串中提取实际类型名称的正则表达式。函数将对类似于<type‘int‘ >的字符串返回 int(其他类型也是如此,如 ‘float‘ 、‘builtin_function_or_method‘ 等)。
注意:你所实现的值将存入类和一些内置类型的__name__属性中。
data = type(.34)
print(data)
type(0)
import re
data = "type ‘int‘ type ‘float‘"
patt = r‘type \‘(\w+)\‘‘
m = re.findall(patt, data)
m
date = ‘10 11 12 09‘
patt = ‘1\d‘
m = re.findall(patt,date)
m
data = ‘1111-111111-11111‘
data2 = ‘1111-1111-1111-1111‘
patt = r‘\d{4}-\d{4}(\d{2})?-\d{4}\d?(-\d{4})?‘
m = re.match(patt, data)
m.group()
import re
data = ‘1111-111111-11111‘
data2 = ‘1111-1111-1111-1111‘
patt = r‘\d{4}-(\d{4}(\d{2})?)-(\d{4}\d?)(-\d{4})?‘
m = re.match(patt, data2)
if m == None:
print(‘Error‘)
elif len(m.group(1))==6 and len(m.group(3))==5:
print(‘Yes, It is 465‘)
elif len(m.group(1))==4 and len(m.group(3))==4:
print(‘Yes, it is 4444‘)
else:
print(‘No‘)
?
#!/usr/bin/env python
#gendata.py
from random import randrange, choice
from string import ascii_lowercase as lc
#from sys import int
from time import ctime
?
tlds = (‘com‘, ‘edu‘, ‘net‘, ‘org‘, ‘gov‘)
for i in range(randrange(5,11)):
dtint = randrange(2**32) #pick date
dtstr = ctime(dtint) #date string
llen = randrange(4, 8) #login is shorter
login = ‘‘.join(choice(lc) for j in range(llen))
dlen = randrange(llen, 13) #domain is longer
dom = ‘‘.join(choice(lc) for j in range(dlen))
print(‘%s::%s@%s.%s::%d-%d-%d‘ % (dtstr, login,
dom, choice(tlds), dtint, llen, dlen))
#!/usr/bin/env python
#gendata.py
from random import randrange, choice
from string import ascii_lowercase as lc
#from sys import int
from time import ctime
?
tlds = (‘com‘, ‘edu‘, ‘net‘, ‘org‘, ‘gov‘)
f = open(‘redata.txt‘,‘w‘)
for i in range(randrange(5,11)):
dtint = randrange(2**32) #pick date
dtstr = ctime(dtint) #date string
llen = randrange(4, 8) #login is shorter
login = ‘‘.join(choice(lc) for j in range(llen))
dlen = randrange(llen, 13) #domain is longer
dom = ‘‘.join(choice(lc) for j in range(dlen))
data = ‘%s::%s@%s.%s::%d-%d-%d\n‘ % (dtstr, login,
dom, choice(tlds), dtint, llen, dlen)
f.write(data)
print(‘%s::%s@%s.%s::%d-%d-%d‘ % (dtstr, login,
dom, choice(tlds), dtint, llen, dlen))
f.close()
#Fweekday = [‘Mon‘,‘Tue‘,‘Wed‘,‘Thu‘,‘Fri‘,‘Sat‘,‘Sun‘]
import re
patt = r‘^\w{3}\s‘
f = open(‘redata.txt‘,‘r‘)
for i in f:
print(re.findall(patt, i.rstrip()))
f.close()
import re
patt = r‘\s\w{3}\s‘
f = open(‘redata.txt‘,‘r‘)
for i in f:
print(re.findall(patt, i.rstrip()))
f.close()
import re
patt = r‘^\w{3}\s\w{3}\s\s?\d{1,2}\s\d{2}:\d{2}:\d{2}‘
f = open(‘redata.txt‘,‘r‘)
for i in f:
print(re.findall(patt, i.rstrip()))
f.close()
import re
patt = r‘^\w{3}\s\w{3}\s\s?\d{1,2}\s\d{2}:\d{2}:\d{2}‘
f = open(‘redata.txt‘,‘r‘)
for i in f:
print(re.findall(patt, i.rstrip()))
f.close()
import re
patt = r‘\w+@\w+\.\w{3}‘
f = open(‘redata.txt‘,‘r‘)
for i in f:
print(re.findall(patt, i.rstrip()))
f.close()
import re
patt = r‘\s\w{3}\s‘
f = open(‘redata.txt‘,‘r‘)
for i in f:
print(re.findall(patt, i.rstrip()))
f.close()
import re
patt = r‘(\d{4}):‘
f = open(‘redata.txt‘,‘r‘)
for i in f:
print(re.findall(patt, i.rstrip()))
f.close()
import re
patt = r‘\d{2}:\d{2}:\d{2}‘
f = open(‘redata.txt‘,‘r‘)
for i in f:
print(re.findall(patt, i.rstrip()))
f.close()
import re
patt = r‘@(\w+\.\w{3})‘
f = open(‘redata.txt‘,‘r‘)
for i in f:
print(re.findall(patt, i.rstrip()))
f.close()
import re
patt = r‘@(\w+)\.(\w{3})‘
f = open(‘redata.txt‘,‘r‘)
for i in f:
print(re.findall(patt, i.rstrip()))
f.close()
import re
patt = r‘\w+@\w+\.\w{3}‘
f = open(‘redata.txt‘,‘r‘)
for i in f:
#print(i)
l = re.sub(patt,‘xx@yy.com‘,i)
print(l)
f.close()
import re
patt = r‘(\w{3})\s\s?(\d{1,2})\s\d{2}:\d{2}:\d{2}\s(\d{4})‘
f = open(‘redata.txt‘,‘r‘)
for i in f:
print(re.findall(patt, i.rstrip()))
f.close()
标签:erro select res uil ada end 集合 而且 号码
原文地址:http://www.cnblogs.com/-Y-C-/p/7634103.html