Skip to main content

before_regex

Returns a string which is the subset of haystack before the match to the regular expression pattern and not including the matched pattern itself.

If no matches to pattern are present in `haystack`, returns an empty string.

If pattern is empty or is an invalid regular expression, throws an exception.

Usage​

{{before_regex text pattern flags="rnt"}}

Arguments​

haystack
The input string.

pattern
A valid regular expression.

flags

  • r: return text before the last match instead of the first match
  • n: return only the text on the same line as the match (from the last newline before the match)
  • t: trim the final result prior to returning (remove all whitespace from beginning and end)

Examples​

Return text before first match of pattern (assuming email.body contains "This is red and that is blue and that is green.")

{{before_regex email.body '/that/' flags='t'}}
This is red and

Return text before last match of pattern (assuming email.body contains "This is red and that is blue and that is green.")

{{before_regex email.body '/that/' flags='tr'}}
This is red and that is blue and

Return an empty string if no match to pattern

{{before_regex email.body '/pizza/' flags='t'}}