Help: Move regex specification to cmake-language(7)

Many other places in CMake beyond the `string` command rely on our
regex syntax, so it should be documented in a more general location.

Fixes: #22270
This commit is contained in:
Tyler Yankee
2026-09-07 10:17:42 -04:00
parent 2ee33f507d
commit 7350b58c16
11 changed files with 96 additions and 83 deletions
+4 -4
View File
@@ -590,8 +590,8 @@ Printing Targets
``REGEX <regex>``
Only list targets whose name matches the given
:ref:`regular expression <Regex Specification>`. If no target matches,
a warning is issued.
:ref:`regular expression <CMake Language Regex Specification>`.
If no target matches, a warning is issued.
``IGNORE_CASE``
Match the ``REGEX`` case-insensitively. Only valid with ``REGEX``.
@@ -646,10 +646,10 @@ Printing Variables
``NAME_REGEX <name-regex>``
Print only variables whose name matches the given
:ref:`regular expression <Regex Specification>`.
:ref:`regular expression <CMake Language Regex Specification>`.
``VALUE_REGEX <value-regex>``
Print only variables whose value matches the given
:ref:`regular expression <Regex Specification>`.
:ref:`regular expression <CMake Language Regex Specification>`.
``IGNORE_CASE``
Lower-case both the pattern and the candidate string before matching,
so ``NAME_REGEX`` and ``VALUE_REGEX`` match case-insensitively.
+1 -1
View File
@@ -114,7 +114,7 @@ Reading
``REGEX <regex>``
Consider only strings that match the given regular expression,
as described under :ref:`string(REGEX) <Regex Specification>`.
as described in the :ref:`CMake Language Regex Specification`.
.. versionchanged:: 3.29
Capture groups from the last match in the file are stored in
+2 -2
View File
@@ -276,8 +276,8 @@ Comparisons
:target: MATCHES
True if the given string or variable's value matches the given regular
expression. See :ref:`Regex Specification` for regex format. ``()`` groups
are captured in :variable:`CMAKE_MATCH_<n>` variables.
expression. See the :ref:`CMake Language Regex Specification` for regex
format. ``()`` groups are captured in :variable:`CMAKE_MATCH_<n>` variables.
.. signature:: if(<variable|string> LESS <variable|string>)
:target: LESS
+1 -1
View File
@@ -805,7 +805,7 @@ Signatures
``REGEX <regex>``
Match any portion of the full path of a file with a
:ref:`regular expression <Regex Specification>`.
:ref:`regular expression <CMake Language Regex Specification>`.
One may use ``/`` and ``$`` to limit matching to the end of a path.
Each ``<match-rule>`` may be followed by ``<match-option>`` arguments.
+2 -2
View File
@@ -135,8 +135,8 @@ Modification
list(FILTER <list> <INCLUDE|EXCLUDE> REGEX <regular_expression>)
For more information on regular expressions look under
:ref:`string(REGEX) <Regex Specification>`.
For more information on regular expressions, see the
:ref:`CMake Language Regex Specification`.
``PREDICATE``
Specify a user-defined :command:`function` as a predicate.
+2 -65
View File
@@ -144,71 +144,8 @@ Search and Replace With Regular Expressions
Regex Specification
"""""""""""""""""""
The following characters have special meaning in regular expressions:
``^``
Matches at beginning of input
``$``
Matches at end of input
``.``
Matches any single character
``\<char>``
Matches the single character specified by ``<char>``. Use this to
match special regex characters, e.g. ``\.`` for a literal ``.``
or ``\\`` for a literal backslash ``\``. Escaping a non-special
character is unnecessary but allowed, e.g. ``\a`` matches ``a``.
``[ ]``
Matches any character(s) inside the brackets.
To match a literal ``]``, make it the first character, e.g., ``[]ab]``.
``[^ ]``
Matches any character(s) not inside the brackets.
To not match a literal ``]``, make it the first character, e.g., ``[^]ab]``.
``-``
Inside brackets, specifies an inclusive range between characters on
either side, e.g., ``[a-f]`` is ``[abcdef]``.
To match a literal ``-`` using brackets, make it the first or the last
character, e.g., ``[+*/-]`` matches basic mathematical operators.
``*``
Matches preceding pattern zero or more times
``+``
Matches preceding pattern one or more times
``?``
Matches preceding pattern zero or once only
``|``
Matches a pattern on either side of the ``|``
``()``
Saves a matched subexpression, which can be referenced
in the ``REGEX REPLACE`` operation.
.. versionadded:: 3.9
All regular expression-related commands, including e.g.
:command:`if(MATCHES)`, save subgroup matches in the variables
:variable:`CMAKE_MATCH_<n>` for ``<n>`` 0..9.
.. noqa: spellcheck off
``*``, ``+`` and ``?`` have higher precedence than concatenation. ``|``
has lower precedence than concatenation. This means that the regular
expression ``^ab+d$`` matches ``abbd`` but not ``ababd``, and the regular
expression ``^(ab|cd)$`` matches ``ab`` but not ``abd``.
.. noqa: spellcheck on
CMake language :ref:`Escape Sequences` such as ``\t``, ``\r``, ``\n``,
and ``\\`` may be used to construct literal tabs, carriage returns,
newlines, and backslashes (respectively) to pass in a regex. For example:
* The quoted argument ``"[ \t\r\n]"`` specifies a regex that matches
any single whitespace character.
* The quoted argument ``"[/\\]"`` specifies a regex that matches
a single forward slash ``/`` or backslash ``\``.
* The quoted argument ``"[A-Za-z0-9_]"`` specifies a regex that matches
any single "word" character in the C locale.
* The quoted argument ``"\\(\\a\\+b\\)"`` specifies a regex that matches
the exact string ``(a+b)``. Each ``\\`` is parsed in a quoted argument
as just ``\``, so the regex itself is actually ``\(\a\+\b\)``. This
can alternatively be specified in a :ref:`bracket argument` without
having to escape the backslashes, e.g. ``[[\(\a\+\b\)]]``.
For more details on CMake's regular expression syntax, please refer to
:ref:`CMake Language Regex Specification`.
Manipulation
^^^^^^^^^^^^
@@ -444,8 +444,8 @@ String Queries
``ALL``
Match as many times as possible and return the matches as a list.
See the :ref:`Regular expressions specification <Regex Specification>` for
the syntax of the ``regular_expression`` parameter.
See the :ref:`CMake Language Regex Specification` for the syntax of the
``regular_expression`` parameter.
.. _`String Generating Generator Expressions`:
+76
View File
@@ -689,3 +689,79 @@ To avoid problems, consider the following advice:
Note that this approach does not apply to :command:`macro` implementations
because they reference arguments using placeholders, not real variables.
.. _`CMake Language Regex Specification`:
Regex Specification
===================
Several CMake commands accept regular expressions, including
:command:`string(REGEX ...) <string>`, :command:`if(MATCHES)`,
:command:`list(FILTER)`, and others, as well as command-line options such as
:option:`ctest -L` and related modes.
The following characters have special meaning in regular expressions:
``^``
Matches at beginning of input
``$``
Matches at end of input
``.``
Matches any single character
``\<char>``
Matches the single character specified by ``<char>``. Use this to
match special regex characters, e.g. ``\.`` for a literal ``.``
or ``\\`` for a literal backslash ``\``. Escaping a non-special
character is unnecessary but allowed, e.g. ``\a`` matches ``a``.
``[ ]``
Matches any character(s) inside the brackets.
To match a literal ``]``, make it the first character, e.g., ``[]ab]``.
``[^ ]``
Matches any character(s) not inside the brackets.
To not match a literal ``]``, make it the first character, e.g., ``[^]ab]``.
``-``
Inside brackets, specifies an inclusive range between characters on
either side, e.g., ``[a-f]`` is ``[abcdef]``.
To match a literal ``-`` using brackets, make it the first or the last
character, e.g., ``[+*/-]`` matches basic mathematical operators.
``*``
Matches preceding pattern zero or more times
``+``
Matches preceding pattern one or more times
``?``
Matches preceding pattern zero or once only
``|``
Matches a pattern on either side of the ``|``
``()``
Saves a matched subexpression, which can be referenced
in the ``REGEX REPLACE`` operation.
.. versionadded:: 3.9
All regular expression-related commands, including e.g.
:command:`if(MATCHES)`, save subgroup matches in the variables
:variable:`CMAKE_MATCH_<n>` for ``<n>`` 0..9.
.. noqa: spellcheck off
``*``, ``+`` and ``?`` have higher precedence than concatenation. ``|``
has lower precedence than concatenation. This means that the regular
expression ``^ab+d$`` matches ``abbd`` but not ``ababd``, and the regular
expression ``^(ab|cd)$`` matches ``ab`` but not ``abd``.
.. noqa: spellcheck on
CMake language :ref:`Escape Sequences` such as ``\t``, ``\r``, ``\n``,
and ``\\`` may be used to construct literal tabs, carriage returns,
newlines, and backslashes (respectively) to pass in a regex. For example:
* The quoted argument ``"[ \t\r\n]"`` specifies a regex that matches
any single whitespace character.
* The quoted argument ``"[/\\]"`` specifies a regex that matches
a single forward slash ``/`` or backslash ``\``.
* The quoted argument ``"[A-Za-z0-9_]"`` specifies a regex that matches
any single "word" character in the C locale.
* The quoted argument ``"\\(\\a\\+b\\)"`` specifies a regex that matches
the exact string ``(a+b)``. Each ``\\`` is parsed in a quoted argument
as just ``\``, so the regex itself is actually ``\(\a\+\b\)``. This
can alternatively be specified in a :ref:`bracket argument` without
having to escape the backslashes, e.g. ``[[\(\a\+\b\)]]``.
+2 -2
View File
@@ -274,8 +274,8 @@ The options for running tests are:
.. option:: -L <regex>, --label-regex <regex>
Run tests with labels matching regular expression as described under
:ref:`string(REGEX) <Regex Specification>`.
Run tests with labels matching regular expression as described in the
:ref:`CMake Language Regex Specification`.
This option tells CTest to run only the tests whose labels match the
given regular expression. When more than one ``-L`` option is given,
+2 -2
View File
@@ -8,8 +8,8 @@
Equivalent to passing :ctest-option:`--tests-regex` on the
command line. This field supports `macro expansion`_.
CMake regex syntax is described under
:ref:`string(REGEX) <Regex Specification>`.
See the :ref:`CMake Language Regex Specification` for the
regex syntax.
.. _`CMakePresets.testPresets.filter.include.label`:
+2 -2
View File
@@ -1434,8 +1434,8 @@ properties:
Equivalent to passing :ctest-option:`--tests-regex` on the
command line. This field supports `macro expansion`_.
CMake regex syntax is described under
:ref:`string(REGEX) <Regex Specification>`.
See the :ref:`CMake Language Regex Specification` for the
regex syntax.
label:
type: string
description: