如何使用多个定界符分割字符串(Python)

 收藏

我试图进一步拆分已经拆分的字符串,以进一步清理它并删除不必要的信息。这是一个用“ /”分隔的URL

['https:', '', 'expressjs.com', 'en', 'starter', 'hello-world.html']

我希望能够做到:

['https:', '', 'expressjs','com', 'en', 'starter', 'hello-world','html']

有什么想法吗?

回复
  • re.split can split a string on any character that matches your regex

    >>> re.split('[/\.]', 'https://expressjs.com/en/starter/hello-world.html')
    ['https:', '', 'expressjs', 'com', 'en', 'starter', 'hello-world', 'html']
    

    [/\.] matches any forward-slash or period character

  • 尝试这个:

    L = ['https:', '', 'expressjs.com', 'en', 'starter', 'hello-world.html']
    L =  [subitem for item in L for subitem in item.split('.')]
    
    print(L)
    

    输出:

    ['https:', '', 'expressjs', 'com', 'en', 'starter', 'hello-world', 'html']