Capture the text inside square brackets using a regex

前端 未结 3 1615
执念已碎
执念已碎 2021-01-21 02:24

I saw question here: Regex to capture {} which is similar to what I want, but I cannot get it to work.

My data is:



        
3条回答
  •  南方客
    南方客 (楼主)
    2021-01-21 02:51

    You can use re.findall to get all the matches, though you'll get them in a list, and you don't need capture groups:

    m = re.findall('\[[^\[\]]*\]', '[Honda] Japanese manufacturer [VTEC] Name of electronic lift control')
    

    Gives ['[Honda]', '[VTEC]'] so you can get each with:

    print(m[0])
    # => [Honda]
    
    print(m[1])
    # => [VTEC]
    

提交回复
热议问题