风火编程--正则表达式应用

正则表达式的推荐使用方式–预编译

import re
s = "12hjk"
pattern = re.compile('.{4}')
r = pattern.match(s)
ret1 = r.group()
print(ret1)

时间格式

def make_time(self, ot):
    """
    把时间转成标准格式字符串
    :param time: 解析出来的时间格式: '2019-09-29 上午/中午/下午8点20分'
    :return: '2019-09-29 08:20'
    """
    t0 = re.sub(r"[点分]", "", ot)
    t1 = t0.split("午")[-1]
    if int(t1) < 1200:
        t2 = t0.replace("中", "上")
    else:
        t2 = t0.replace("中", "下")
    t3 = t2.replace("上午", "AM").replace("下午", "PM")
    t4 = datetime.datetime.strptime(t3, "%Y-%m-%d %p%I%M")
    return str(t4)

统一单位

def handle_unit(uo):
    """
    单位转换
    :param uo: 单位不同的时间数据ms/s/M/H
    :return:同一单位的时间ms
    """
    pattern = re.compile("^(\d*\.?\d*)(.*)$")
    r = pattern.match(uo)
    t, u = float(r.group(1)), r.group(2)
    d = {
        "ms": t,
        "s": t*1000,
        "M": t*60*1000,
        "H": t*60*60*1000
    }
    t_r = d[u]

    print(t_r)

匹配js的function

pattern_sub_js = re.compile(r"\(function.*?\}\).*?;", re.S)

正则切割符

import re
pattern = re.compile(r"\(\w*\)")
s = "hjkhjkh(你好)jmk(呀)"
r = pattern.split(s)
print(r)

正则捕获组

s = "1986-09-01"
r = re.sub('(\d{4})-(\d{2})-(\d{2})', r'\2/\3/\1', s)
print(r)

正则替换去空白

s = """
1  
2   
    
3 
4    
5
"""

r = re.sub(r'\s', "", s)
print(r)

你可能感兴趣的:(python应用)