error_get_last() doesn't show last warning Drupal has its own custom error handler, so when I try to get the message from the last warning (e.g. for an invalid regex), there is nothing there:

if (@preg_match($regex, NULL) === FALSE) {
  dpm(error_get_last());
}


The @ prevents it from being displayed on the screen, and without that I can see a warning for a $regex of '/(/' (about an unmatched paren), but the warning is not shown by dpm(), but rather by Drupal.

Is there a way I can get the warning from an invalid regex in this example, so that I could have the message in a variable?
Use the same PHP's error functions as Drupal to toggle PHP's error handling behavior. 

In particular, you can use set_error_handler() / restore_error_handler() to toggle the PHP error code handling while you're debugging:

// Remove _drupal_error_handler() handling
restore_error_handler();

if (@preg_match($regex, NULL) === FALSE) {
  dpm(error_get_last());
}
// Restore _drupal_error_handler() handling
set_error_handler('_drupal_error_handler');


See the PHP doc links for more information on how these error handling functions work.