Q:

Regex two special characters in a row

I have this regex
\[.+\]\(.+\)
Why does it match this string entirely?
[test1](test1) thisbitshouldnotmatch [test2](test2)

It should only match [test1](test1) and [test2](test2). thisbitshouldnotmatch should not match.

A:

That is because the + operator is greedy.
For expression \[.+\]\(.+\) the characters are matched as follows:
[test1](test1) thisbitshouldnotmatch [test2](test2)
[..........................................](.....)

so, whole input matches!
You'd need to either use nongreedy:
\[.+?\]\(.+?\)

Or, explicitery disallow some characters
\[[^\]]+\]\([^)]+\)

(notice how I replaced the catch-any . with a character group that excludes ] or ) respectively)`

A:

Try with this expression:
\[.+?\]\(.+?\)

That will limit the result so it only matches the first occurrence of [] and of (). Notice that by default an expression such as this: .+ will try to match as much of the input as possible. By adding a ? quantifier at the end: .+? we're specifying that the search should stop at the first match it finds.

A:

You need to make the dot lazy otherwise it will grab everything it can:
\[.+?]\(.+?\)

Or, even better, use a negated character class, so a [ followed by many not ] followed by a ]
\[[^]]++]\([^)]++\)

Also note that you don't need to escape ]

