How to extract a substring from Variable in grafana promql

When I select NRT Instance say sp1nrt, sp1cap I want to load the bucket with label bucket_name=sp1. How to extract the same from the from the variable probably by regex.

Try this :
bucket_size(
size_type=“used”,
bucket_name=~“${nrtInstance:regex/^(sp[0-9]+).*/$1/}”
)
or

create a second variable and keep your panel query clean:

  1. Dashboard → Settings → Variables → Add variable
  2. Name: nrtPrefix
  3. Type: Query
  4. Datasource: Prometheus
  5. Query type: Label values
  6. Label: bucket_name
  7. Regex:
/^(sp[0-9]+)/
  1. Save

Now in your panel just do:

promql

bucket_size(size_type="used", bucket_name="$nrtPrefix")

Either of these will ensure that when you pick sp1nrt or sp1cap in $nrtInstance, Grafana only ever sends sp1 to your bucket_name filter.