正则表达式

正则表达式的定义

元字符和普通字符

字符集

s = 'abc, acc, adc, aec, afc, ahc'
r = re.findall('a[^cfC]c', s) # 表示已a开头,c结尾,中间的一个字母不是c,f,C中的任何一个
print(r) # ['abc', 'adc', 'aec', 'ahc']

概括字符集

数量词

贪婪和非贪婪

# 匹配3-6个字母,贪婪模式下,会匹配尽可能多的字母,直到不满足条件,因此会匹配出['python', 'java', 'php']
# 非贪婪模式下会匹配3个字符,【'pyt', 'hon', 'jav', 'php']
a = 'python 111java678php'
r = re.findall('[a-z]{3,6}', a) # 贪婪模式
r = re.findall('[a-z]{3,6}?', a) # 非贪婪模式,加一个问号

匹配 0 次,1 次或无限次

边界匹配符

匹配模式参数

s = 'abc, acc, adc, aec, afc, ahc, aFc'
r = re.findall('a[cf]c', s, re.I) # 忽略大小写
print(r) # ['acc', 'afc', 'aFc']

re.sub 正则替换

a = 'python 111java678php'
r1 = re.sub('\w{3,6}', "hhh", a, 1)  # 最后一个参数1表示替换几个

print(r1)  # hhh 111java678php


def convert(value):
    return 'aa' + value.group()  # value.group()为匹配到的字符串


# 第二个参数为一个函数,匹配到后会把匹配的结果传给函数执行,并把函数返回的结果作为替换字符串
r2 = re.sub('\w{3,6}', convert, a)
print(r2)  # aapython 111java678php

match 与 search 函数

group 分组

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` (默认第0个分组为字符串本身)
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 ') (不会返回完整字符串)

JSON

理解 JSON

JSON 的特点

反序列化

1

import json
# 这里必须是里面是双引号,外面是单引号,false和true首字符必须小写
s = '{"name": "ljl", "age": 12, "flag": true}'
a = json.loads(s)  # Python中会把JSON字符串解析成字典或列表
print(a) # {'name': 'ljl', 'age': 12, 'flag': True}

序列化

s1 = json.dumps(a)
print(s1)

枚举

枚举类型(Python3)

from enum import Enum

class VIP(Enum):
    YELLOW = 1
    GREEN = 2
    BLACK = 3
    RED = 4

print(VIP.YELLOW)  # VIP.YELLOW
print(VIP.YELLOW == VIP.GREEN)  # false

枚举的优势

from enum import Enum

class VIP(Enum):
    YELLOW = 1
    GREEN = 2
    BLACK = 3
    RED = 4
    # BLACK = 5  # error
    YELLOW_ALIAS = 1 # YELLOW的别名


print(VIP.YELLOW)  # VIP.YELLOW
print(VIP.YELLOW == VIP.GREEN)  # false
VIP.YELLOW = 3  # error,枚举的值不能改变

枚举的操作

print(VIP.YELLOW)  # VIP.YELLOW,枚举类型
print(VIP.YELLOW.value)  # 1,枚举的值
print(VIP.YELLOW.name)  # YELLOW,枚举的名字

# 枚举的遍历,遍历时别名不会遍历
# VIP.YELLOW
# VIP.GREEN
# VIP.BLACK
# VIP.RED
for v in VIP:
    print(v)

# 遍历枚举的所有值,包括别名
for v in VIP.__members__:
    print(v)

# 枚举的比较
print(VIP.YELLOW == VIP.GREEN) # false
print(VIP.YELLOW == VIP.YELLOW) # false
print(VIP.YELLOW == 1)  # false
# print(VIP.YELLOW < VIP.GREEN)  # error,枚举不可以进行大小比较
print(VIP.YELLOW.value == 1)  # true
print(VIP.YELLOW == VIP.YELLOW_ALIAS)  # true

枚举的转换

class VIP(Enum):
    YELLOW = 1
    GREEN = 2
    BLACK = 3
    RED = 4


a = VIP(1)  # a 初始化为VIP.YELLOW
print(a) # VIP.YELLOW
print(a == VIP.YELLOW)  # true