Template help with before and after characters

I’m usually pretty good at finding out how to do jinja templates for text manipulation but this one is throwing me for a loop. My kids have stories they listen to during bedtime and I’m trying to get the specific parts of the text for different use cases.

Example of the text: The Secret of the Worry Dolls :nesting_dolls::heart_hands: Children’s Story For Anxious Sleepers

The story title is in the first part before the emoticons and there is a random string of text after them. I’m looking to get something that cuts out the text after the emoticons so I can put this into a card. I’m also looking to get just the string before the emoticons (without the emoticons) so I can have the Google sdk announce the story. I’m stuck at how to do either regex or something else for getting the text I’m after in the example.

Thank you in advance for any help.

I’m unsure if there is a better way to do this but this is what I’ve come up with:

{{
(states.sensor.kokosleep.attributes.entries[states.input_number.kajasroomkoko.state
  | int].title | urlencode | replace('%20',' ') | replace('%27',"'") | replace('%2C',',') | replace('%26','&') | replace('%21','!') | replace('%3F','?') | regex_findall('.+?(?=%[0-9a-fA-F]+)'))[0] | trim
}}

I’m converting the string using urlencode and replacing the characters I want and using regex to grab the text before the emoji url encoded string. Not the best way but I couldn’t figure out how to get jinja’s regex filters to match on the emojis. Still open to any suggestions though.

{% set t = "The Secret of the Worry Dolls 👌 Children’s Story For Anxious Sleepers" %}
{{ (t|regex_findall("[\s\w]*"))[0] }}

Alternative:

{% set t = "The Secret of the Worry Dolls 👌 Children’s Story For Anxious Sleepers" %}
{% set o = t|map('ord')|list %}
{% set e = o|select('>',2000)|first %}
{{ t[:o.index(e)] }}

Adjust the 2000 to match the character code boundary at which you want to stop.

1 Like

Thank you! The alternative version worked great. The first solution wouldn’t work because it split on the ’ and other characters. Regardless though, this gives me something to dissect in the near future to better understand what it’s doing. Thank you again.