Skip to content Skip to sidebar Skip to footer

Capture The Text Inside Square Brackets Using A Regex

I saw question here: Regex to capture {} which is similar to what I want, but I cannot get it to work. My data is: [Honda] Japanese manufacturer [VTEC] Name of electronic lift cont

Solution 1:

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]']

Solution 2:

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]

Solution 3:

If you are considering other than re:

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

Output:

['Honda', 'VTEC']

Post a Comment for "Capture The Text Inside Square Brackets Using A Regex"