Think about what you mean and translate that into the regex language. As Gumbo has pointed out, you should be using [^:]
instead of .
; the reason for this is that you are looking for groups of characters that aren't colons ([^:]
), not for groups of absolutely any character at all[1] (.
) which happen to have colons between them.
Any time you find yourself using .
with a quantifier in a regex, stop and ask yourself whether you really mean "any character" or whether you could express your meaning more clearly (and get more accurate results) using a character class instead.
(Non-greedy quantifiers (.*?
) can also do the job of getting correct matches in cases like this, but character classes are still a clearer expression of intent for human readers and improve efficiency by avoiding excessive backtracking for machine readers.)
[1] Well, absolutely any character at all, with the possible exception of newlines depending on the regex implementation that you're using.