slug 필드에 넣을 값을 만들어보자

2021. 4. 16. 13:06카테고리 없음

	objs = {}
        p_han = re.compile('[가-힣]+')
        match_iter = re.finditer(p_han, title)
        for match_obj in match_iter:
            objs[match_obj.start()] = match_obj.group()

        p_eng = re.compile('[a-zA-Z]+')
        match_iter = re.finditer(p_eng, title)
        for match_obj in match_iter:
            objs[match_obj.start()] = match_obj.group()

        p_num = re.compile('[0-9]+')
        match_iter = re.finditer(p_num, title)
        for match_obj in match_iter:
            objs[match_obj.start()] = match_obj.group()

        p_blank = re.compile(' +')
        match_iter = re.finditer(p_blank, title)
        for match_obj in match_iter:
            objs[match_obj.start()] = match_obj.group()

        slug = ''
        for (key, value) in sorted(objs.items()):
            slug += value
        slug = slug.strip().replace(' ', '-')