# Find and replace filter

**URL:** <https://forum.ansible.com/t/find-and-replace-filter/14131>\
**Category:** Ansible Project\
**Created:** [December 11, 2013, 8:16pm UTC](https://forum.ansible.com/t/find-and-replace-filter/14131 "2013-12-11T20:16:27Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_Byerly](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@Matt\_Byerly](https://forum.ansible.com/u/Matt_Byerly)\
**Post date:** [December 11, 2013, 8:16pm UTC](https://forum.ansible.com/t/find-and-replace-filter/14131/1 "2013-12-11T20:16:27Z")

</div>

Heyo,

I was looking for a built-in filter either in Ansible or in Jinja2 that supported regex based find and replace. It doesn’t appear a real straight forward implementation exists today. I was able to achieve what I wanted by adding the following bit to ‘core.py’ for testing purposes.

def regex\_replace(s, find, replace):  
return re.sub(find, replace, s)

I was then able to take an existing variable do what I wanted.

i.e.

bgp router-id {{external\_ipv4\_self\_ip|regex\_replace(“[/].\*”, “”)}}
