Help with Regex in Template

Hoping someone can help, I suck at regex.
The current template I am working with is
{{ state_attr(‘media_player.squeeze_rxstereo’, ‘media_content_id’)}}

This gives an output of “qobuz://track/19512577”
All I want from this is qobuz. Of course this might be spotify on another track, and the track number at the back end of the output is a random length of characters. So I cant just regex the first 5 characters, or remove last 17.

So what I need is a regex that removes the “:” and everything past it.
Bonus if I can also force a capital first letter “Qobuz”.

No need for regex if the bit you want is always separated by :

{{ state_attr('media_player.squeeze_rxstereo', 'media_content_id').split(':')[0]|title }}

split(':') splits the string into bits everywhere : appears and puts the bits in a list.

[0] selects the first item in the list

|title gives you the title case you asked for.

1 Like

And all of that without any regex :slight_smile:

Thanks a ton!
I assume split would work with any character, like .split(‘/’) . Then it would spit it everywhere a “/” occurs?

tom_l’s suggestion is shorter but if you want an example using regex, here’s how:

{{ state_attr('media_player.squeeze_rxstereo', 'media_content_id')|regex_replace(':.*', '')|title}}

Yes.

Reference

Python split()

I’m still a novice, but this might work:

{{ state_attr('media_player.squeeze_rxstereo', 'media_content_id') | regex_findall('(.*?)(?:\:[^:]*)$') }}

That regex pattern takes the long road to get “qobuz” out of the supplied string. You just need to discard everything after the colon.

It also reports it as a list, not a string, so you also need to extract the list’s first item and then filter it with title.

1 Like

':.*' Elegant.