<p>This works for me just fine:</p>

<pre><code>{# Removes all characters other than numbers and + #}
{{ profile.phone|replace('/[^0-9+]/', '') }}
</code></pre>

CC BY-SA 3.0
2016-11-18T05:47:35.430Z
2016-11-18T05:47:35.430Z
medoingthings
<h2>Answers for 2023 using Craft CMS 4</h2>
<p><strong>For simple find and replace:</strong> you can just use the <a href="https://craftcms.com/docs/4.x/dev/filters.html#replace" rel="nofollow noreferrer">replace filter</a>, it accepts RegEx now.</p>
<p><strong>For more complicated lookups:</strong> If you're looking to just add Craft CMS filters in general or use PHP filters, like <code>preg_match_all()</code> you can install the <a href="https://plugins.craftcms.com/twig-toolbox?craft4" rel="nofollow noreferrer">Twig Toolbox plugin</a>, create the file <code>config/twig-toolbox.php</code> with the following code to enable the filter or add more as needed:</p>
<pre><code>&lt;?php return [
    'filters' =&gt; [
        'preg_match_all' =&gt; function(string $subject, string $pattern): array {
            preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER);
            return $matches;
        },
    ],
    'functions' =&gt; [],
    'globals' =&gt; [],
    'tests' =&gt; [],
];
</code></pre>
<p>Please note, the above filter is a basic version of the actual <code>preg_match_all</code>, see the <a href="https://www.php.net/manual/en/function.preg-match-all.php" rel="nofollow noreferrer">full documentation</a> and adapt as you see fit.</p>

CC BY-SA 4.0
2023-05-02T15:34:45.240Z
2023-05-02T15:34:45.240Z
franklylately
<p>Currently Twig uses <a href="https://php.net/manual/en/pcre.pattern.php">regular expressions</a> only with <a href="http://twig.sensiolabs.org/doc/templates.html#comparisons">Comparisons</a>:</p>

<pre><code>{% if string matches '{^[\d\.]+$}' %}
    Do Stuff
{% endif %}
</code></pre>

<p>Craft itself currently doesn't have any custom Twig <a href="https://buildwithcraft.com/docs/templating/filters">filters</a> or <a href="https://buildwithcraft.com/docs/templating/functions">functions</a> either.</p>

<p>But there are third-party plugins which might help you: </p>

<ul>
<li><a href="https://github.com/victor-in/Craft-TwigPCRE">Twig PCRE Filters</a></li>
<li><a href="https://github.com/low/low_regex/">Low Regex</a></li>
</ul>

CC BY-SA 3.0
2015-03-23T18:48:45.103Z
As of Craft 2.2, the `replace` filter [now supports regular expressions...](http://buildwithcraft.com/docs/templating/filters#replace)
Lindsey D
CC BY-SA 3.0
2014-06-16T18:34:49.427Z
2014-06-16T18:34:49.427Z
Victor
<p>Is it possible to perform a RegEx match on a string in Twig?</p>

<p>I'm looking for something like this:</p>

<pre><code>{% set string = "This is **string**. **wer**  qewr **234**" %}
{% set match  = string.matches(/([*][*])(.*)([*][*])/g) %}
</code></pre>

CC BY-SA 3.0
2014-06-16T12:55:02.843Z
This question is a better fit the on StackOverflow site.
Anna_MediaGirl
CC BY-SA 3.0
2014-06-16T13:02:14.687Z
@Anna_MediaGirl now?
nicael
CC BY-SA 3.0
2014-06-16T13:47:59.667Z
I believe you'd have to extend twig for this. [This](https://github.com/jasny/twig-extensions#pcre) looks like it has what you'd need, specifically the `preg_get_all`. If you're trying to bold that text though, there's a [`markdown`](http://buildwithcraft.com/docs/templating/filters#markdown-or-md) filter that can do that for you.
Bill Criswell
CC BY-SA 3.0
2014-06-16T14:42:32.263Z
Yes, your edit makes the question relevant to Craft.
Anna_MediaGirl
CC BY-SA 3.0
2014-06-16T17:43:34.567Z
Here's a preg_replace implementation in a Craft plugin: https://github.com/low/low_regex/. This should give you a framework to build what you need. Nice if the 'on hold' comes off your original question, and good fortune.
narration_sd
CC BY-SA 3.0
2014-06-16T17:55:57.850Z
To open the question we need one more vote to reopen it. It's beside the edit and flag link.
Victor
<templating><regex>
How can I perform a RegEx match in Twig?
# How can I perform a RegEx match in Twig?

(Asked by: username_0 on 2014-06-16)

Is it possible to perform a RegEx match on a string in Twig?
I'm looking for something like this:
{% set string = "This is **string**. **wer**  qewr **234**" %}
{% set match  = string.matches(/([*][*])(.*)([*][*])/g) %}

**Tags:** templating, regex

> **Comments:**
> **username_1**: This question is a better fit the on StackOverflow site.  
> **username_0**: @username_1 now?  
> **username_2**: I believe you'd have to extend twig for this. [This](https://github.com/jasny/twig-extensions#pcre) looks like it has what you'd need, specifically the `preg_get_all`. If you're trying to bold that text though, there's a [`markdown`](http://buildwithcraft.com/docs/templating/filters#markdown-or-md) filter that can do that for you.  
> **username_1**: Yes, your edit makes the question relevant to Craft.  
> **username_3**: Here's a preg_replace implementation in a Craft plugin: https://github.com/low/low_regex/. This should give you a framework to build what you need. Nice if the 'on hold' comes off your original question, and good fortune.  
> **username_4**: To open the question we need one more vote to reopen it. It's beside the edit and flag link.  

---

## 3 answers

### Answer by username_4 on 2014-06-16. Score: 21 | Accepted answer

Currently Twig uses regular expressions only with Comparisons:
{% if string matches '{^[\d\.]+$}' %}
    Do Stuff
{% endif %}

Craft itself currently doesn't have any custom Twig filters or functions either.
But there are third-party plugins which might help you: 

Twig PCRE Filters
Low Regex

> **Comments:**
> **username_7**: As of Craft 2.2, the `replace` filter [now supports regular expressions...](http://buildwithcraft.com/docs/templating/filters#replace)  

---

### Answer by username_5 on 2016-11-18. Score: 5

This works for me just fine:
{# Removes all characters other than numbers and + #}
{{ profile.phone|replace('/[^0-9+]/', '') }}


---

### Answer by username_6 on 2023-05-02. Score: 1

Answers for 2023 using Craft CMS 4
For simple find and replace: you can just use the replace filter, it accepts RegEx now.
For more complicated lookups: If you're looking to just add Craft CMS filters in general or use PHP filters, like preg_match_all() you can install the Twig Toolbox plugin, create the file config/twig-toolbox.php with the following code to enable the filter or add more as needed:
 [
        'preg_match_all' => function(string $subject, string $pattern): array {
            preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER);
            return $matches;
        },
    ],
    'functions' => [],
    'globals' => [],
    'tests' => [],
];

Please note, the above filter is a basic version of the actual preg_match_all, see the full documentation and adapt as you see fit.


---

