Capture the text inside square brackets using a regex

前端 未结 3 1614
执念已碎
执念已碎 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]
    
    0 讨论(0)
  • 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']
    
    0 讨论(0)
  • 2021-01-21 03:01

    You only have one group in your expression, so you can only ever get that one group. Group 1 is the capturing group, group 0 is the whole matched text; in your expression they are one and the same. Had you omitted the (...) parentheses, you'd only have a group 0.

    If you wanted to get all matches, use re.findall(). This returns a list of matching groups (or group 0, if there are no capturing groups in your expression):

    >>> import re
    >>> re.findall('\[[^\[\]]*\]', '[Honda] Japanese manufacturer [VTEC] Name of electronic lift control')
    ['[Honda]', '[VTEC]']
    
    0 讨论(0)
提交回复
热议问题