Answers.Body: <p>This works for me just fine:</p>
  
  <pre><code>{# Removes all characters other than numbers and + #}
  {{ profile.phone|replace('/[^0-9+]/', '') }}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-18T05:47:35.430Z
Answers.LastActivityDate: 2016-11-18T05:47:35.430Z
Answers.UserDisplayName: medoingthings
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-05-02T15:34:45.240Z
Answers.LastActivityDate: 2023-05-02T15:34:45.240Z
Answers.UserDisplayName: franklylately
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-23T18:48:45.103Z
Answers.Text: As of Craft 2.2, the `replace` filter [now supports regular expressions...](http://buildwithcraft.com/docs/templating/filters#replace)
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-16T18:34:49.427Z
Answers.LastActivityDate: 2014-06-16T18:34:49.427Z
Answers.UserDisplayName: Victor
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-16T12:55:02.843Z
Comments.Text: This question is a better fit the on StackOverflow site.
Comments.UserDisplayName: Anna_MediaGirl
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-16T13:02:14.687Z
Comments.Text: @Anna_MediaGirl now?
Comments.UserDisplayName: nicael
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-16T13:47:59.667Z
Comments.Text: 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.
Comments.UserDisplayName: Bill Criswell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-16T14:42:32.263Z
Comments.Text: Yes, your edit makes the question relevant to Craft.
Comments.UserDisplayName: Anna_MediaGirl
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-16T17:43:34.567Z
Comments.Text: 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.
Comments.UserDisplayName: narration_sd
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-16T17:55:57.850Z
Comments.Text: To open the question we need one more vote to reopen it. It's beside the edit and flag link.
Comments.UserDisplayName: Victor
Tags: <templating><regex>
Title: How can I perform a RegEx match in Twig?
ThreadText: # 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](https://php.net/manual/en/pcre.pattern.php) only with [Comparisons](http://twig.sensiolabs.org/doc/templates.html#comparisons):
  
  ```
  {% if string matches '{^[\d\.]+$}' %}
      Do Stuff
  {% endif %}
  
  ```
  
  Craft itself currently doesn't have any custom Twig [filters](https://buildwithcraft.com/docs/templating/filters) or [functions](https://buildwithcraft.com/docs/templating/functions) either.
  
  But there are third-party plugins which might help you:
  
  * [Twig PCRE Filters](https://github.com/username_4-in/Craft-TwigPCRE)
  * [Low Regex](https://github.com/low/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](https://craftcms.com/docs/4.x/dev/filters.html#replace), 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](https://plugins.craftcms.com/twig-toolbox?craft4), create the file `config/twig-toolbox.php` with the following code to enable the filter or add more as needed:
  
  ```
  <?php return [
      'filters' => [
          '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](https://www.php.net/manual/en/function.preg-match-all.php) and adapt as you see fit.
  
  
  ---
  
  