2018-08-17

一、初识正则表达式

正则表达式 是一个特殊的字符序列,一个字符串是否与我们所设定的这样的字符序列,相匹配

快速检索文本、实现替换文本的操作

json(xml) 轻量级 web 数据交换格式

[[图片上传失败...(image-1b4425-1534603173151)]](javascript:void(0); "复制代码")

import re

a='C|C++|Java|C#||Python|Javascript' r= re.findall('Python',a) print(r) if len(r) > 0: print('字符串中包含Python') else: print('No')

['Python']
字符串中包含Python

[[图片上传失败...(image-ca7b51-1534603173150)]](javascript:void(0); "复制代码")

二、元字符与普通字符

[[图片上传失败...(image-d096d6-1534603173150)]](javascript:void(0); "复制代码")

import re

a='C0C++7Java8C#9Python6Javascript' r= re.findall('\d',a) print(r)

b=''
for x in a: try:
int(x)
b +=x+','
except : pass

print(b)

结果:
['0', '7', '8', '9', '6']
0,7,8,9,6,

[[图片上传失败...(image-3925ae-1534603173150)]](javascript:void(0); "复制代码")

'Python' 普通字符 '\d' 元字符

三、字符集

[[图片上传失败...(image-2a3853-1534603173150)]](javascript:void(0); "复制代码")

import re #找出中间一个字符不是C 和F的 单词
s = 'abc, acc, adc, aec, afc, ahc' r = re.findall('a[^cf]c', s) #[a-z] [cf]
print(r)

结果:
['abc', 'adc', 'aec', 'ahc']

[[图片上传失败...(image-41d22e-1534603173150)]](javascript:void(0); "复制代码")

四、概括字符集

[[图片上传失败...(image-f112f4-1534603173150)]](javascript:void(0); "复制代码")

#\d 数字 \D 字母 #\w 数字和字母 =[a-zA-Z0-9_] \W

\s 空白字符 \S

a='python 11\t11java&678p\nh\rp' r = re.findall('\s', a) print(r)

结果:
[' ', '\t', '\n', '\r']

[[图片上传失败...(image-ad7a61-1534603173150)]](javascript:void(0); "复制代码")

五、数量词

[[图片上传失败...(image-a9fa95-1534603173150)]](javascript:void(0); "复制代码")

a='python 1111java&678php' r = re.findall('[a-z]{3,6}', a) print(r)

结果:
['python', 'java', 'php']

[[图片上传失败...(image-f12747-1534603173150)]](javascript:void(0); "复制代码")

六、贪婪与非贪婪

[[图片上传失败...(image-3f84a0-1534603173150)]](javascript:void(0); "复制代码")

a='python 1111java&678php' r = re.findall('[a-z]{3,6}?', a) #贪婪 与 非贪婪 ?

print(r)

结果:
['pyt', 'hon', 'jav', 'php']

[[图片上传失败...(image-cf12d9-1534603173150)]](javascript:void(0); "复制代码")

七、匹配0次1次或者无限多次

[[图片上传失败...(image-cee55c-1534603173150)]](javascript:void(0); "复制代码")

# * 匹配0次或者无限多次 # + 匹配1次或者无限多次 # ? 匹配0次或者1次
a='pytho0python1pythonn2pythonw' r = re.findall('python*', a) print(r)

结果:
['pytho', 'python', 'pythonn', 'python']

[[图片上传失败...(image-d3d1c-1534603173150)]](javascript:void(0); "复制代码")

八、边界匹配符

[[图片上传失败...(image-99a089-1534603173150)]](javascript:void(0); "复制代码")

qq = '12345678'

4~8

r = re.findall('^\d{4,8}/pre>, qq) print(r)

a = '123456789'

4~8 ^规则结尾

e = re.findall('^\d{4,8}/pre>, a) print(e)

结果:
['12345678']
[]

[[图片上传失败...(image-de4c57-1534603173150)]](javascript:void(0); "复制代码")

九、组

[[图片上传失败...(image-8cec14-1534603173150)]](javascript:void(0); "复制代码")

# () 组
a = 'pythonpythonpythonpythonpython'

r = re.findall('(python){3}', a) print(r)

结果:
['python'] 代表存在一组(pythonpythonpython) 这样的数据

[[图片上传失败...(image-1e2436-1534603173150)]](javascript:void(0); "复制代码")

十、匹配模式参数

[[图片上传失败...(image-78930b-1534603173150)]](javascript:void(0); "复制代码")

# I | S 忽略大小写 | 匹配所有字符
lanuage = 'PythonC#\nJavaPHP' r = re.findall('c#.{1}', lanuage,re.I | re.S) print(r)

结果:
['C#\n']

[[图片上传失败...(image-bd66b9-1534603173150)]](javascript:void(0); "复制代码")

十一、re.sub正则替换

搜索替换

[[图片上传失败...(image-265746-1534603173149)]](javascript:void(0); "复制代码")

def convert(value):
matched = value.group() # print(value) <_sre.SRE_Match object; span=(6, 8), match='C#'>
return '!!'+matched+'!!' lanuage = 'PythonC#JavaC#PHPC#'

r = re.sub('C#', 'GO', lanuage, 1) 返回结果: PythonGOJavaC#PHPC# # s=lanuage.replace('C#', 'GO')

r = re.sub('C#', convert, lanuage) #传入参数
print(r)

结果:
Python!!C#!!Java!!C#!!PHP!!C#!!

[[图片上传失败...(image-79f6fa-1534603173149)]](javascript:void(0); "复制代码")

十二、把函数作为参数传递

[[图片上传失败...(image-83e85b-1534603173149)]](javascript:void(0); "复制代码")

def convert(value):
matched = value.group() #拿到对象的值
# print(value) <_sre.SRE_Match object; span=(6, 8), match='C#'>
if int(matched) >=6 : return '9'
else: return '0' lanuage = 'A8C3721D86' r = re.sub('\d', convert, lanuage) print(r) # A9C0900D99

[[图片上传失败...(image-f5ba96-1534603173149)]](javascript:void(0); "复制代码")

十三、search与match函数

[[图片上传失败...(image-5c3683-1534603173149)]](javascript:void(0); "复制代码")

s = 'A8C3721D86'

None 从开头开始匹配 假如没有找到相应的匹配结果 返回None 只匹配一次

r = re.match('\d', s) print(r) #None

搜索这个字符串 一旦找到第一个满足匹配的结果就返回 只匹配一次

r1 = re.search('\d', s) print(r1) #<_sre.SRE_Match object; span=(1, 2), match='8'>
print(r1.group()) #8
print(r1.span()) # (1, 2)
r2 = re.findall('\d', s) print(r2) #['8', '3', '7', '2', '1', '8', '6']

[[图片上传失败...(image-d35d8a-1534603173149)]](javascript:void(0); "复制代码")

十四、group分组

[[图片上传失败...(image-2fb148-1534603173149)]](javascript:void(0); "复制代码")

#提取life 和python 之间的值
s = 'life is short,i use python'

None

r = re.search('life.python', s) print(r.group()) #life is short,i use python group(组号)
r = re.search('life(.
)python', s) print(r.group(0)) #life is short,i use python group(组号)
print(r.group(1)) # is short,i use

group(0) 一种特殊情况 匹配正则表达式完整的结果

r = re.findall('life(.*)python', s) print(r) #[' is short,i use ']

[[图片上传失败...(image-7de018-1534603173149)]](javascript:void(0); "复制代码")

[[图片上传失败...(image-111bd4-1534603173149)]](javascript:void(0); "复制代码")

s = 'life is short,i use python, i love python' r = re.search('life(.)python(.)python', s) print(r.group(0)) # life is short,i use python, i love python
print(r.group(1)) # is short,i use
print(r.group(2)) # , i love

print(r.group(0,1,2)) #('life is short,i use python, i love python', ' is short,i use ', ', i love ')

print(r.groups()) # (' is short,i use ', ', i love ')

[[图片上传失败...(image-e6d227-1534603173149)]](javascript:void(0); "复制代码")

十五、一些关于学习正则的建议

[[图片上传失败...(image-6f5d9f-1534603173149)]](javascript:void(0); "复制代码")

#\d 数字 \D 字母 #\w 数字和字母 =[a-zA-Z0-9_] \W

\s 空白字符 \S # . 匹配除了换行符\n之外其他所有字符 # * 匹配0次或者无限多次 # + 匹配1次或者无限多次 # ? 匹配0次或者1次 # () 组

I | S 忽略大小写 | 匹配所有字符

[[图片上传失败...(image-758b5b-1534603173149)]](javascript:void(0); "复制代码")

python :爬虫,数据处理

十六、理解JSON

JSON 是一种轻量级的数据交换格式

字符串是JSON的表现形式

符合 JSON 格式的字符串叫做 JSON 字符串

{"name":"qiyue"}

JSON VS XML

优势:

跨语言交换数据

易于阅读

易于解析

网络传输效率高

十七、反序列化

[[图片上传失败...(image-9043d1-1534603173149)]](javascript:void(0); "复制代码")

import json # JSON object array
json_str = '{"name":"qiyue","age":18}' s = json.loads(json_str) # dict #反序列化
s = json.loads(json_str) #load() 把json 的数据类型 转换为我们自己语言的数据类型
print(type(s)) #
print(s) #{'name': 'qiyue', 'age': 18}
print(s['name']) # qiyue
json_str = '[{"name":"qiyue","age":18},{"name":"qiyue","age":18}]' s = json.loads(json_str) print(type(s)) #
print(s) # [{'name': 'qiyue', 'age': 18}, {'name': 'qiyue', 'age': 18}]

[[图片上传失败...(image-f24d86-1534603173149)]](javascript:void(0); "复制代码")

[[图片上传失败...(image-6bd3a0-1534603173149)]](javascript:void(0); "复制代码")

JSON Python
object dict
array list
string str
number int
number float
true True
false False
null None

[[图片上传失败...(image-6c3b4f-1534603173149)]](javascript:void(0); "复制代码")

十八、序列化

[[图片上传失败...(image-eb536b-1534603173149)]](javascript:void(0); "复制代码")

#序列化 为json
student = [
{"name":"qiyue","age":18, 'flag':False},
{"name":"python","age":18}
]

json_str = json.dumps(student) print(type(json_str)) #
print(json_str) #[{"name": "qiyue", "age": 18, "flag": false}, {"name": "python", "age": 18}]

[[图片上传失败...(image-922f15-1534603173149)]](javascript:void(0); "复制代码")

十九、小谈JSON、JSON对象与JSON字符串

JSON 是一种轻量级的数据交换格式

JSON对象 局限于语言

JSON字符串

JSON 有自己的数据类型

虽然它和JavaScript 的数据类型有些相似 但是他们不是一种语言

ECMASCRIPT一个标准 JavaScript ActionScription JSON 实现标准的一种方案

REST 服务

你可能感兴趣的:(2018-08-17)