String slugification in Python

前端 未结 10 1079
粉色の甜心
粉色の甜心 2020-11-30 23:10

I am in search of the best way to \"slugify\" string what \"slug\" is, and my current solution is based on this recipe

I have changed it a little bit to:

<         


        
10条回答
  •  Happy的楠姐
    2020-11-30 23:51

    You might consider changing the last line to

    slug=re.sub(r'--+',r'-',slug)
    

    since the pattern [-]+ is no different than -+, and you don't really care about matching just one hyphen, only two or more.

    But, of course, this is quite minor.

提交回复
热议问题