Templating variables

We’re storing the metrics in Graphite in the format




How do I define the queries for 2 variables $ServiceName and $App such that $App takes all possible values of the 4th component (*.*.*.*) and $ServiceName takes all possible values that contain $App in the 4th component?
So for the example above, if I set $App to kafka-producer, I want $ServiceName to suggest settingservice and searchservice.

Using Grafana v4.6.1 (commit: cac8b97)