COOLak
COOLak

Reputation: 399

How to define "ends with any two letters after underscore" in Twig

In the following example, this condition includes all type elements of an array that do not include _.

{% for type in array %}
    {% if '_' not in type) %}

Instead, I would like to include all elements that do not end with _any2letters, where "any2letters" is actual any 2 letters. I examined Twig documentation and wasn't able to find the required syntax.

Upvotes: 1

Views: 272

Answers (1)

COOLak
COOLak

Reputation: 399

It was solved using:

{% if not (type matches '/_[a-z]{2}$/') %}

This solution takes advantage of PHP regex syntax.

  • "/" are used in Twig to define borders of regular expressions.
  • "_" is my underscore as is; [a-z]{2} means "2 lowercase letters".
  • And finally $ means that the preceding symbols are in the end of the string.

Upvotes: 1

Related Questions