Regular expression to match SQL placeholders

If you ever need to pick out SQL placeholders, this pattern will do it:

(?x)
(
(['"`]) ((?!\\g2).)+ \\g2(*SKIP)
| \\?\\d*
|\\$\\d+
)(?<=[^"'`])

So, for example, if you have 'somebody?' = ?, this pattern will ignore the quoted “?” and match only the unquoted one.

It will also work if the placeholder is in the form of “?1” or “$1”.