| 1 |
.. _id.appendix.regular_expressions: |
| 2 |
|
| 3 |
============================================================================== |
| 4 |
Regular Expressions |
| 5 |
============================================================================== |
| 6 |
|
| 7 |
.. index:: regular expressions, regexp |
| 8 |
|
| 9 |
The following tables provide a overview of the `regular expressions`_ syntax. |
| 10 |
See also `Python regular expressions`_ description in the Python `re module`_. |
| 11 |
|
| 12 |
|
| 13 |
===================== ========================================================= |
| 14 |
Special Characters Description |
| 15 |
===================== ========================================================= |
| 16 |
``.`` Matches any character (dot). |
| 17 |
``^`` "^...", matches start-of-string (caret). |
| 18 |
``$`` "...$", matches end-of-string (dollar sign). |
| 19 |
``|`` "A|B", matches "A" or "B". |
| 20 |
``\`` Escape character. |
| 21 |
``\.`` EXAMPLE: Matches character '.' (dot). |
| 22 |
``\\`` EXAMPLE: Matches character '``\``' (backslash). |
| 23 |
===================== ========================================================= |
| 24 |
|
| 25 |
To select or match characters from a special set of characters, |
| 26 |
a character set must be defined. |
| 27 |
|
| 28 |
===================== ========================================================= |
| 29 |
Character sets Description |
| 30 |
===================== ========================================================= |
| 31 |
``[...]`` Define a character set, like ``[A-Za-z]``. |
| 32 |
``\d`` Matches digit character: [0-9] |
| 33 |
``\D`` Matches non-digit character. |
| 34 |
``\s`` Matches whitespace character: ``[ \t\n\r\f\v]`` |
| 35 |
``\S`` Matches non-whitespace character |
| 36 |
``\w`` Matches alphanumeric character: ``[a-zA-Z0-9_]`` |
| 37 |
``\W`` Matches non-alphanumeric character. |
| 38 |
===================== ========================================================= |
| 39 |
|
| 40 |
A text part must be group to extract it as part (parameter). |
| 41 |
|
| 42 |
===================== ========================================================= |
| 43 |
Grouping Description |
| 44 |
===================== ========================================================= |
| 45 |
``(...)`` Group a regular expression pattern (anonymous group). |
| 46 |
``\number`` Matches text of earlier group by index, like: "``\1``". |
| 47 |
``(?P<name>...)`` Matches pattern and stores it in parameter "name". |
| 48 |
``(?P=name)`` Match whatever text was matched by earlier group "name". |
| 49 |
``(?:...)`` Matches pattern, but does non capture any text. |
| 50 |
``(?#...)`` Comment (is ignored), describes pattern details. |
| 51 |
===================== ========================================================= |
| 52 |
|
| 53 |
If a *group*, *character* or *character set* should be repeated several times, |
| 54 |
it is necessary to specify the cardinality of the regular expression pattern. |
| 55 |
|
| 56 |
===================== ============================================================== |
| 57 |
Cardinality Description |
| 58 |
===================== ============================================================== |
| 59 |
``?`` Pattern with cardinality 0..1: optional part (question mark). |
| 60 |
``*`` Pattern with cardinality zero or more, 0.. (asterisk). |
| 61 |
``+`` Pattern with cardinality one or more, 1.. (plus sign). |
| 62 |
``{m}`` Matches ``m`` repetitions of a pattern. |
| 63 |
``{m,n}`` Matches from ``m`` to ``n`` repetitions of a pattern. |
| 64 |
``[A-Za-z]+`` EXAMPLE: Matches one or more alphabetical characters. |
| 65 |
===================== ============================================================== |
| 66 |
|
| 67 |
|
| 68 |
.. _`regular expressions`: https://en.wikipedia.org/wiki/Regular_expression |
| 69 |
.. _Python regular expressions: https://docs.python.org/2/library/re.html#module-re |
| 70 |
.. _re module: https://docs.python.org/2/library/re.html#module-re |
| 71 |
|