You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Metacharacters ``\1`` through ``\9`` are interpreted as backreferences to groups.
413
+
They match the previously found group with the specified index.
415
414
416
415
=========== ============================
417
416
RegEx Matches
@@ -420,15 +419,28 @@ RegEx Matches
420
419
``(.+)\1+`` also ``abab`` and ``123123``
421
420
=========== ============================
422
421
423
-
``(['"]?)(\d+)\1`` matchs ``"13"`` (in double quotes), or ``'4'`` (in
424
-
single quotes) or ``77`` (without quotes) etc
422
+
RegEx ``(['"]?)(\d+)\1`` matches ``"13"`` (in double quotes), or ``'4'`` (in
423
+
single quotes) or ``77`` (without quotes) etc.
424
+
425
+
Named Groups and Backreferences
426
+
-------------------------------
427
+
428
+
To make some group (ie subexpression) named, use this syntax: ``(?P<name>)``. Name of group must be valid identifier: first char is letter or "_", other chars are alphanumeric or "_". All named groups are also usual groups and share the same numbers 1 to 9.
429
+
430
+
Backreferences to named groups are ``(?P=name)``, the numbers ``\1`` to ``\9`` can also be used.
Modifiers are for changing behaviour of regular expressions.
430
442
431
-
You can set modifiers globally in your system or change inside the the
443
+
You can set modifiers globally in your system or change inside the
432
444
regular expression using the `(?imsxr-imsxr) <#inlinemodifiers>`_.
433
445
434
446
.. note::
@@ -547,45 +559,26 @@ RegEx Matches
547
559
548
560
The modifier is set `On` by default.
549
561
550
-
Extensions
562
+
Assertions
551
563
----------
552
564
553
-
.. _lookahead:
565
+
.. _assertions:
554
566
555
-
(?=<lookahead>)
556
-
~~~~~~~~~~~~~~~
567
+
Currently engine supports only these kinds of assertions:
557
568
558
-
``Look ahead`` assertion. It checks input for the regular expression
559
-
``<look-ahead>``, but do not capture it.
569
+
Positive lookahead assertion: ``foo(?=bar)`` matches "foo" only before "bar", and "bar" is excluded from the match.
560
570
561
-
.. note::
562
-
`TRegExpr <tregexpr.html>`__
571
+
Positive lookbehind assertion: ``(?<=foo)bar`` matches "bar" only after "foo", and "foo" is excluded from the match.
563
572
564
-
Look-ahead is not implemented in TRegExpr.
573
+
Assertions are allowed only at the very beginning and ending of expression. They can contain subexpressions of any complexity (quantifiers are allowed, even groups are allowed). Lookahead and lookbehind can be present both.
565
574
566
-
In many cases you can replace ``look ahead`` with
567
-
`Sub-expression <#subexpression>`_ and just ignore what will be
568
-
captured in this subexpression.
575
+
Non-capturing Groups
576
+
--------------------
569
577
570
-
For example ``(blah)(?=foobar)(blah)`` is the same as ``(blah)(foobar)(blah)``.
571
-
But in the latter version you have to exclude the middle sub-expression
572
-
manually - use ``Match[1] + Match[3]`` and ignore ``Match[2]``.
578
+
Syntax is like this: ``(?:subexpression)``.
573
579
574
-
This is just not so convenient as in the former version where you can use
575
-
whole ``Match[0]`` because captured by ``look ahead`` part would not be
576
-
included in the regular expression match.
577
-
578
-
.. _inlinemodifiers:
579
-
580
-
581
-
(?:<non-capturing group>)
582
-
~~~~~~~~~~~~~~~~~~~~~~~~~
583
-
584
-
``?:`` is used when you want to group an expression, but you do not want to
585
-
save it as a matched/captured portion of the string.
586
-
587
-
So this is just a way to organize your regex into subexpressions without
588
-
overhead of capturing result:
580
+
Such groups do not have the "index" and are invisible for backreferences.
581
+
Non-capturing groups are used when you want to group a subexpression, but you do not want to save it as a matched/captured portion of the string. So this is just a way to organize your regex into subexpressions without overhead of capturing result:
0 commit comments