Outer aliasSub regex not replacing correclty

This is not working as expected:

First, the good behavior without the 2nd aliastSub():

sortByName(grep(aliasSub(divideSeriesLists(#B, #D),'^.*host=([^;)]+).*','$Summary; host=\1'),'average'))

produces two series labels:
* {count,min,average,median,percentile,max}; host=fsrvbt-ob-133
* {count,min,average,median,percentile,max}; host=p272

Adding the outer aliasSub() the results are not as expected:

aliasSub(sortByName(grep(aliasSub(divideSeriesLists(#B, #D),'^.*host=([^;)]+).*','$Summary; host=\1'),'average')),'^.* host=(.*)','\1 - average')

Produces the following labels:
* fsrvbt-ob-133
* p272

However the expected output is:
* fsrvbt-ob-133 - average
* p272 - average

No matter what the formatting string is, Grafana produces the same series labels, even:

aliasSub(sortByName(grep(aliasSub(divideSeriesLists(#B, #D),'^.*host=([^;)]+).*','$Summary; host=\1'),'average')),'^.* host=(.*)','f')

* fsrvbt-ob-133
* p272

Is there a way to work around this?

When I test this on https://regex101.com/ with inputs:

  • regular expression: ^.* host=(.*)
  • test string: {count,min,average,median,percentile,max}; host=fsrvbt-ob-133
  • substitution:\1 - average

I get the expected result:

  • fsrvbt-ob-133 - average

I managed to work around it by removing any spaces in the first aliasSub() and matching on the curly brackets in the second:

aliasSub(grep(aliasSub(divideSeriesLists(#B, #D),'^.*host=([^;)]+).*','$Summary\1'),'average'),'^[^\}]+\}([^\{\}]+)','\1 - average')