$re = '/instance\[(\d+)\]/m';
$str = 'hi everyone,
i\'d like to take a large block of text and find every instance[0] of
a certain word, in this case, instance[1] - and i\'d like to replace
every instance[2] number with another number, let\'s say [3].
string.replace doesn\'t quite cut it..
anyone able to help?
';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php