Skip to content

Instantly share code, notes, and snippets.

@SpacePossum
Last active December 19, 2023 13:25
Show Gist options
  • Save SpacePossum/b71821aab939438e654ebc9fc1714bd8 to your computer and use it in GitHub Desktop.
Save SpacePossum/b71821aab939438e654ebc9fc1714bd8 to your computer and use it in GitHub Desktop.
Example how Twig `(not) matches` works
{% set foo = '123' %}
{{ foo matches '/^[\\d]+$/' ? 'Y' : 'N' }}
{{ foo matches not '/^[\\d]+$/' ? 'Y' : 'N' }} {# wrong, does not work! #}
{{ not (foo matches '/^[\\d]+$/') ? 'Y' : 'N' }}
{% set foo = 'abc' %}
{{ foo matches '/^[\\d]+$/' ? 'Y' : 'N' }}
{{ foo matches not '/^[\\d]+$/' ? 'Y' : 'N' }} {# wrong, does not work! #}
{{ not (foo matches '/^[\\d]+$/') ? 'Y' : 'N' }}
Y
N
N
N
N
Y
@michaelKaefer
Copy link

Thank you! :)

@ghalusa
Copy link

ghalusa commented Jul 12, 2020

Nice! Thanks!

@PhilETaylor
Copy link

Awesome - thanks for sharing this, it helped me today :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment