You can use regex and a capture group. See this answer to an Influx query.
And here is an answer for Prometheus: