Answers.Body: <p>Craft's <code>replace</code> filter will just use PHP's <code>preg_replace</code> method behind the scenes if it detects that a regular expression has been passed in.</p>
  
  <p>It sounds like you're looking for the PHP <a href="https://secure.php.net/manual/en/function.preg-match.php" rel="nofollow noreferrer">preg_match</a> method, which isn't available natively.</p>
  
  <p>There was <a href="https://github.com/victor-sm/Craft-TwigPCRE" rel="nofollow noreferrer">a plugin</a> that added support for this in Craft 2, but I don't think it's been ported to Craft 3. You might be able to use that as a starting point if you'd like to port it, though.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-28T23:34:27.980Z
Answers.Text: I just created a Craft 3 version of this plugin with a few more filters. Still in testing before submission to plugin store but can be installed via composer command. https://github.com/indigoviking/expressive
Answers.UserDisplayName: Chattervast
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-27T01:22:24.430Z
Answers.LastActivityDate: 2018-12-27T01:22:24.430Z
Answers.UserDisplayName: Brad Bell
Body: <p>Using <code>replace</code> filter we can match pattern using regular expressions, but can we use part of the matched string in the second argument of filter? For example, I want to match all numbers and replace each matched number with a matched number enclosed in parentheses.</p>
  
Tags: <templating>
Title: Twig replace filter - use part of regex match
ThreadText: # Twig replace filter - use part of regex match
  
  (Asked by: username_0 on 2018-12-26)
  
  Using `replace` filter we can match pattern using regular expressions, but can we use part of the matched string in the second argument of filter? For example, I want to match all numbers and replace each matched number with a matched number enclosed in parentheses.
  
  **Tags:** templating
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-12-27. Score: 2 | Accepted answer
  
  Craft's `replace` filter will just use PHP's `preg_replace` method behind the scenes if it detects that a regular expression has been passed in.
  
  It sounds like you're looking for the PHP [preg\_match](https://secure.php.net/manual/en/function.preg-match.php) method, which isn't available natively.
  
  There was [a plugin](https://github.com/victor-sm/Craft-TwigPCRE) that added support for this in Craft 2, but I don't think it's been ported to Craft 3. You might be able to use that as a starting point if you'd like to port it, though.
  
  > **Comments:**
  > **username_2**: I just created a Craft 3 version of this plugin with a few more filters. Still in testing before submission to plugin store but can be installed via composer command. https://github.com/indigoviking/expressive  
  
  ---
  
  