# I want to remove a string from the legend format in Grafana

**URL:** <https://community.grafana.com/t/i-want-to-remove-a-string-from-the-legend-format-in-grafana/60346>\
**Category:** Prometheus\
**Tags:** templating\
**Created:** [February 7, 2022, 7:09am UTC](https://community.grafana.com/t/i-want-to-remove-a-string-from-the-legend-format-in-grafana/60346 "2022-02-07T07:09:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![murarisaranvikas](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@murarisaranvikas](https://community.grafana.com/u/murarisaranvikas)\
**Post date:** [February 7, 2022, 7:09am UTC](https://community.grafana.com/t/i-want-to-remove-a-string-from-the-legend-format-in-grafana/60346/1 "2022-02-07T07:09:52Z")

</div>

![Screenshot 2022-02-07 at 12.36.02 PM](https://us1.discourse-cdn.com/grafana/original/3X/c/a/ca9e055ee750a3e6b57e230beac2513f0a6afeaa.png)

I want to remove \_custom\_exporter from all the metrics being present on the Grafana dashboard.

How do i put this in legend format?

i should display only OS names.  
like ubuntu20\_04 instead of ubuntu20\_04\_custom\_exporter.

Can you please help me?

---

<div class="post-metadata">

**Author:** ![melori.arellano](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/melori.arellano/32/23041_2.png) [@melori.arellano](https://community.grafana.com/u/melori.arellano)\
**Post date:** [February 8, 2022, 6:19pm UTC](https://community.grafana.com/t/i-want-to-remove-a-string-from-the-legend-format-in-grafana/60346/2 "2022-02-08T18:19:36Z")

</div>

Hi @murarisaranvikas, welcome to the community!

[This](https://community.grafana.com/t/how-to-rename-values-in-grafana-using-regex-based-value-mapping/53616/4) post in the community has a lot of great information about how to use the [value mapping feature](https://grafana.com/docs/grafana/latest/panels/value-mappings/#map-a-regular-expression) with regex to edit values that show up in your graph.

I think we could definitely use more examples, so I mocked up your query with CSV data and  
a bar chart to show an example of how you could remove the end of the value name in your bar chart.

To transform this:

 ![Screen Shot 2022-02-08 at 10.56.07 AM](https://us1.discourse-cdn.com/grafana/original/3X/a/e/aea2e052aa783e66bccf4f4253646cd2edd3b959.jpeg)

Into this, removing the `_exporter` string at the end:

 ![Screen Shot 2022-02-08 at 10.42.37 AM](https://us1.discourse-cdn.com/grafana/original/3X/b/8/b8942d3746dc7f4f9f648bf5921b3a7491fdceaa.jpeg)

You can use a value mapping in the **bar gauge options** in the sidebar of the panel editor.

 ![Screen Shot 2022-02-08 at 10.44.27 AM](https://us1.discourse-cdn.com/grafana/original/3X/0/0/0006cbabfb6886672191e637f8b32b92f31910ca.png)

When you click **Add value mappings** it opens with a default **Value** mapping not **Regex**.

Click the **Add a value mapping** button, and choose **Regex** from the dropdown to add the regex value mappng and substitution. You can delete the default value mapping.

 ![Screen Shot 2022-02-08 at 11.07.34 AM](https://us1.discourse-cdn.com/grafana/original/3X/4/1/41de7ecf9bf6c04624c1429fdf585f3fb9240107.jpeg)

The regex `(.*)_.*` will capture everything up to the last underscore and `$1` will substitute that as the new value.

 ![Screen Shot 2022-02-08 at 11.07.51 AM](https://us1.discourse-cdn.com/grafana/original/3X/c/e/cec92526f2e0af91fbd9d07f1d3e2a4f4159f4b5.png)

The site [https://regex101.com/](https://regex101.com/) is a really helpful tool to test out regex. You’ll want to change the **flavor** to golang to get the right interpretations for Grafana.

---

<div class="post-metadata">

**Author:** ![jler](https://avatars.discourse-cdn.com/v4/letter/j/87869e/32.png) [@jler](https://community.grafana.com/u/jler)\
**Post date:** [June 13, 2023, 6:38am UTC](https://community.grafana.com/t/i-want-to-remove-a-string-from-the-legend-format-in-grafana/60346/3 "2023-06-13T06:38:07Z")

</div>

Hi there. Thanks for the great explanation! Unfortunately I have a similar problem but the regex doesn’t work. I need to get the last part of the string as legend entry. Although it is working great on [https://regex101.com/](https://regex101.com/), it is still not working in Grafana:

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/8/1/81a64bbe5da0054d597625ed618c0cc052b423c0.png)

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/f/d/fde40ac4a8f33a7e91194a0f9ce5e1c98807bc17.png)

What I am doing wrong?

Thanks ans regards  
JLER

---

<div class="post-metadata">

**Author:** ![jimbob](https://avatars.discourse-cdn.com/v4/letter/j/13edae/32.png) [@jimbob](https://community.grafana.com/u/jimbob)\
**Post date:** [July 13, 2023, 8:36am UTC](https://community.grafana.com/t/i-want-to-remove-a-string-from-the-legend-format-in-grafana/60346/4 "2023-07-13T08:36:12Z")

</div>

This works for me if I use the Transform option in the panel.  
 ![image](https://us1.discourse-cdn.com/grafana/original/3X/4/3/43d33544b19685771ff8fd2298280c9dbd99ba18.png)

I am trying to do something similar, but remove a string before a character. Any thoughts on the regex?

---

<div class="post-metadata">

**Author:** ![fadjar340](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/fadjar340/32/10236_2.png) [@fadjar340](https://community.grafana.com/u/fadjar340)\
**Post date:** [July 13, 2023, 1:43pm UTC](https://community.grafana.com/t/i-want-to-remove-a-string-from-the-legend-format-in-grafana/60346/5 "2023-07-13T13:43:24Z")

</div>

Hi,

what is your data and what do want to achieve?

Regards,  
Fadjar

---

<div class="post-metadata">

**Author:** ![jimbob](https://avatars.discourse-cdn.com/v4/letter/j/13edae/32.png) [@jimbob](https://community.grafana.com/u/jimbob)\
**Post date:** [July 13, 2023, 8:03pm UTC](https://community.grafana.com/t/i-want-to-remove-a-string-from-the-legend-format-in-grafana/60346/6 "2023-07-13T20:03:20Z")

</div>

We found a solution.  
I wanted to remove the text after the last space:  
`some text the-rest-of-it` essentially needed to keep `the-rest-of-it` and remove `some text `

we managed it using this transform:  
 ![image](https://us1.discourse-cdn.com/grafana/original/3X/4/9/491c640bf371b0736736fcefd566ccb24ad0f431.png)

---

<div class="post-metadata">

**Author:** ![rdanielo](https://avatars.discourse-cdn.com/v4/letter/r/a87d85/32.png) [@rdanielo](https://community.grafana.com/u/rdanielo)\
**Post date:** [October 11, 2023, 9:26am UTC](https://community.grafana.com/t/i-want-to-remove-a-string-from-the-legend-format-in-grafana/60346/7 "2023-10-11T09:26:40Z")

</div>

Having this same exact problem. I already setup the mapping:

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/8/3/83606674bdcc8238e08f5709f34741201ca6bc22.jpeg)

But the labels are not being mapped

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/e/3/e3411e44c64430829ab6ab16a37fcc731bdbbae7.png)

I checked that the regex indeed matches well in regex101

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/1/c/1c1f40f3bb074f2bcbec6d6fe51f52062e375c06.png)

My data source is netdata from prometheus

---

<div class="post-metadata">

**Author:** ![rdanielo](https://avatars.discourse-cdn.com/v4/letter/r/a87d85/32.png) [@rdanielo](https://community.grafana.com/u/rdanielo)\
**Post date:** [October 11, 2023, 10:08am UTC](https://community.grafana.com/t/i-want-to-remove-a-string-from-the-legend-format-in-grafana/60346/8 "2023-10-11T10:08:48Z")

</div>

Got it working. Turns out what I wanted was a transform, not a value mapping:

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/5/9/5943b2f802997f3ff3643daab2c4f6952432164e.png)
