Templating variables

We’re storing the metrics in Graphite in the format

<service_name>.<service_version>.<service_host>.<app>

e.g.

settingservice.1_7_0000374_8b6e984.setting-malli.kafka-producer
searchservice.1_8_0000354_8b32985.search-foo.kafka-producer

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)