Capture the text inside square brackets using a regex

前端 未结 3 1652
执念已碎
执念已碎 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:59

    If you are considering other than re:

    s="[Honda] Japanese manufacturer [VTEC] Name of electronic lift control"
    result = []
    tempStr = ""
    flag = False
    for i in s:
        if i == '[':
            flag = True
        elif i == ']':
            flag = False
        elif flag:
            tempStr = tempStr + i
        elif tempStr != "":
            result.append(tempStr)
            tempStr = ""
    
    print result
    

    Output:

    ['Honda', 'VTEC']
    

提交回复
热议问题