I have a string in the form of:
"[NUM : NAME : NUM]: [NUM : NAME : NUM]:..."
I want to be able to extract all the NAMEs out of this string. The NAME can have any character, ranging from alphabet to punctuation symbols and numbers. NUM is only in the form of [0-9]+
I tried issuing this command:
re.findall(r"\[[0-9]+\:([.]+)\:[0-9]+\]", string)
But instead of giving what I requested, it would bunch up a few [NUM : NAME : NUM]s into the [.]+ group, which is also correct in terms of this regex, but not what I need.
Any help would be much appreciated.
Try this:
Adding the
?after the+is non-greedy. Greedy means that the+will take as many characters as possible while still matching and it is greedy by default. By adding the?you are telling it to be non-greedy, which means the+will take the minimum number of characters to match.The above will work if there are no spaces between num, :, and name.
If there are spaces then use: