Q:

Preg_replace is not working correctly

So here is my string:
$tmpl = "...TABLE __? ( `id` int(11...."

And here is a preg_replace:
$tmpl = preg_replace('/([^"\'0-9a-z_])__([a-z_]+[^"\'])/i', "\$1".$this->config->db_prefix2."\$2", $tmpl);

By the way $this->config->db_prefix2 is equal to "list_"
But when the code executes, preg_replace does nothing(leaves everything without changes).  It seems to me, that the problem is in preg_replace regular expression.  The idea is to
replace __?  to list_?.  

A:

Im not sure why you have such a complex pattern when you can just do this
 $tmpl = str_replace('__?',  $this->config->db_prefix2, $tmpl);

Or if you have to use preg_replace
 $tmpl = string_replace('/__\?/', $this->config->db_prefix2, $tmpl);

You can test it here 
https://regex101.com/r/pufIoY/1
also don't blame be because you left the table name out

$tmpl = "...TABLE __? ( id int(11...."

That said, If I was templating this, I would use something like this 
CREATE TABLE {DbPrefix}.{Table} ( id int(11...."

And then just use, str_replace('{DbPrefix}', $prefix ).   Mainly because it's more readable then __?.    But it's your party.