preg_match在树枝上

First of all, I know that the logic should be in the controller and not in the view and I keep it that way.

But in this particular situation I need to use preg_match within a ternary operation to set the css class of a div.

Example:

{% for list in lists %}
    <div class="{{ (preg_match(list.a, b))>0 ? something : else }}"...>...</div>
{% endfor %}

How can I achieve the (preg_match(list.a,b))>0 condition in twig?

Thanks in advance

You can't use the preg_match directly but there are ways to accomplish it

1) if list is an entity add a method matches {{ (list.matches(b)) ? something : else }}

2) you could create a custom twig extension function that uses preg_match internally http://symfony.com/doc/master/cookbook/templating/twig_extension.html

For those who came here from Google search results (like me).

There's containment operator that allows you to do something like this:

{{ 'cd' in 'abcde' }} {# returns true #}