Alert query regex and backreferencing

Hi,

I understand that I can’t use Template Variables for alerting purposes but I can use a regex in the query to monitor state on multiple hosts. Given a query like this:

round(100*sum(pg_stat_database_blks_hit{hostname=“test-postgres-1”}) / (sum(pg_stat_database_blks_hit{hostname=“test-postgres-1”}) + sum(pg_stat_database_blks_read{hostname=“test-postgres-1”})),0.1)

Is it possible to replace the initial “test-postgres-1” with a regex like /test-postgres-[1-5]/ and then backreference it from the other two hostname conditionals? If not, it there another way to accomplish this without having a dedicated query for every host?

Many thanks
Steve