Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve docs on regex filters #67133

Open
wants to merge 1 commit into
base: 3006.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions doc/topics/jinja/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,9 @@ This text will be wrapped in quotes.

.. versionadded:: 2017.7.0

Scan through string looking for a location where this regular expression
produces a match. Returns ``None`` in case there were no matches found
Looks for a match for the specified regex anywhere in the string. If the string
does not match the regex, this filter returns ``None``. If the string _does_
match the regex, then the `capture groups`_ for the regex will be returned.

Example:

Expand All @@ -420,6 +421,29 @@ Returns:

("defabcdef",)

If the regex you use does not contain a capture group then the number of
capture groups will be zero, and a matching regex will return an empty tuple.
This means that the following ``if`` statement would evaluate as ``False``:

.. code-block:: jinja

{%- if 'foobar' | regex_search('foo') %}

If you do not need a capture group and are just looking to test if a string
matches a regex, then you should check to see if the filter returns ``None``:

.. code-block:: jinja

{%- if (some_var | regex_search('foo')) is not none %}

.. note::

In a Jinja statement, a null value (i.e. a Python ``None``) should be
expressed as ``none`` (i.e. lowercase). More info on this can be found in
the **Note** section here in the `jinja docs`_.

.. _`capture groups`: https://docs.python.org/3/library/re.html#re.Match.groups
.. _`jinja docs`: https://jinja.palletsprojects.com/en/stable/templates/#literals

.. jinja_ref:: regex_match

Expand All @@ -428,8 +452,8 @@ Returns:

.. versionadded:: 2017.7.0

If zero or more characters at the beginning of string match this regular
expression, otherwise returns ``None``.
Works exactly like :jinja_ref:`regex_search`, but only checks for matches at
the _beginning_ of the string passed into this filter.

Example:

Expand Down
Loading