Using "replace" in template

I’m sure this is easy for you programmer folks but I have been googling and can’t seem to find this proper way to have wildcards in replace… example being if I want to remove a zip code from a string in my template I have

|replace('55555','')

how could I get it to replace either like 55*** or just any group of 5 numbers after my state abbreviation. Like: XX #####
(I may or may not end up wanting to remove the state)

Any help would be appreciated… sorry if this is something super simple just not finding a basic enough example in the Jinja2 docs… they all seem to reference specific strings.

3 Likes

You probably want a regular expression. Try regex_replace.

This simple example worked in the template editor:

{{ "a 15555 b" |regex_replace(find='\d{5}', replace='XXXXX', ignorecase=False) }}

Outputs

a XXXXX b

\d{5} means replace a sequence of 5 numbers.

4 Likes

thanks so much!