2017-11-02 03:03:24 +01:00
|
|
|
= Regex
|
2017-10-13 07:14:31 +02:00
|
|
|
|
2021-05-10 08:40:01 +02:00
|
|
|
== Regex syntax
|
2017-10-13 07:14:31 +02:00
|
|
|
|
2021-10-29 12:09:19 +02:00
|
|
|
Kakoune's regex syntax is inspired after ECMAScript, as defined by the
|
2021-05-10 08:40:01 +02:00
|
|
|
ECMA-262 standard (see <<regex#compatibility,:doc regex compatibility>>).
|
2017-10-13 07:14:31 +02:00
|
|
|
|
2021-05-10 08:40:01 +02:00
|
|
|
Kakoune's regex always runs on Unicode codepoint sequences, not on bytes.
|
2017-10-13 07:14:31 +02:00
|
|
|
|
2017-11-02 03:03:24 +01:00
|
|
|
== Literals
|
2017-10-13 07:14:31 +02:00
|
|
|
|
|
|
|
Every character except the syntax characters `\^$.*+?[]{}|().` match
|
2021-05-10 08:40:01 +02:00
|
|
|
themselves. Syntax characters can be escaped with a backslash so that
|
|
|
|
`\$` will match a literal `$`, and `\\` will match a literal `\`.
|
2017-10-13 07:14:31 +02:00
|
|
|
|
2017-10-20 06:08:24 +02:00
|
|
|
Some literals are available as escape sequences:
|
2017-10-13 07:14:31 +02:00
|
|
|
|
|
|
|
* `\f` matches the form feed character.
|
2021-05-10 08:40:01 +02:00
|
|
|
* `\n` matches the newline character.
|
2017-10-13 07:14:31 +02:00
|
|
|
* `\r` matches the carriage return character.
|
|
|
|
* `\t` matches the tabulation character.
|
2017-10-13 08:42:58 +02:00
|
|
|
* `\v` matches the vertical tabulation character.
|
2017-10-20 06:08:24 +02:00
|
|
|
* `\0` matches the null character.
|
2021-05-10 08:40:01 +02:00
|
|
|
* `\cX` matches the control-`X` character (`X` can be in `[A-Za-z]`).
|
|
|
|
* `\xXX` matches the character whose codepoint is `XX` (in hexadecimal).
|
|
|
|
* `\uXXXXXX` matches the character whose codepoint is `XXXXXX` (in hexadecimal).
|
2017-10-13 07:14:31 +02:00
|
|
|
|
2017-11-02 03:03:24 +01:00
|
|
|
== Character classes
|
2017-10-13 07:14:31 +02:00
|
|
|
|
2017-10-16 03:35:03 +02:00
|
|
|
The `[` character introduces a character class, matching one character
|
|
|
|
from a set of characters.
|
2017-10-13 07:14:31 +02:00
|
|
|
|
|
|
|
A character class contains a list of literals, character ranges,
|
|
|
|
and character class escapes surrounded by `[` and `]`.
|
|
|
|
|
|
|
|
If the first character inside a character class is `^`, then the character
|
|
|
|
class is negated, meaning that it matches every character not specified
|
|
|
|
in the character class.
|
|
|
|
|
|
|
|
Literals match themselves, including syntax characters, so `^`
|
2020-07-09 11:58:43 +02:00
|
|
|
does not need to be escaped in a character class. `[\*+]` matches both
|
|
|
|
the `\*` character and the `+` character. Literal escape sequences are
|
2021-05-10 08:40:01 +02:00
|
|
|
supported, so `[\n\r]` matches both the newline and carriage return
|
2017-10-13 07:14:31 +02:00
|
|
|
characters.
|
|
|
|
|
|
|
|
The `]` character needs to be escaped for it to match a literal `]`
|
|
|
|
instead of closing the character class.
|
|
|
|
|
|
|
|
Character ranges are written as `<start character>-<end character>`, so
|
2021-05-10 08:40:01 +02:00
|
|
|
`[A-Z]` matches all uppercase basic letters. `[A-Z0-9]` will match all
|
|
|
|
uppercase basic letters and all basic digits.
|
2017-10-13 07:14:31 +02:00
|
|
|
|
|
|
|
The `-` characters in a character class that are not specifying a
|
|
|
|
range are treated as literal `-`, so `[A-Z-+]` matches all upper case
|
|
|
|
characters, the `-` character, and the `+` character.
|
|
|
|
|
2017-10-13 08:42:58 +02:00
|
|
|
Supported character class escapes are:
|
2017-10-13 07:14:31 +02:00
|
|
|
|
2021-06-09 09:50:03 +02:00
|
|
|
* `\d` which matches digits 0-9.
|
|
|
|
* `\w` which matches word characters A-Z, a-z, 0-9 and underscore
|
|
|
|
(ignoring the `extra_word_chars` option).
|
|
|
|
* `\s` which matches all Unicode whitespace characters.
|
|
|
|
* `\h` which matches whitespace except Vertical Tab and line-breaks.
|
2017-10-13 07:14:31 +02:00
|
|
|
|
2017-10-13 08:42:58 +02:00
|
|
|
Using an upper case letter instead of a lower case one will negate
|
2021-05-10 08:40:01 +02:00
|
|
|
the character class. For example, `\D` will match every non-digit
|
|
|
|
character.
|
2017-10-13 07:14:31 +02:00
|
|
|
|
2017-10-13 08:42:58 +02:00
|
|
|
Character class escapes can be used outside of a character class, `\d`
|
2017-10-13 07:14:31 +02:00
|
|
|
is equivalent to `[\d]`.
|
|
|
|
|
2017-11-02 03:03:24 +01:00
|
|
|
== Any character
|
2017-10-13 07:14:31 +02:00
|
|
|
|
2021-05-10 08:40:01 +02:00
|
|
|
`.` matches any character, including newlines, by default.
|
|
|
|
(see <<regex#modifiers,:doc regex modifiers>> on how to change it)
|
2017-10-13 07:14:31 +02:00
|
|
|
|
2017-11-02 03:03:24 +01:00
|
|
|
== Groups
|
2017-10-13 07:14:31 +02:00
|
|
|
|
|
|
|
Regex atoms can be grouped using `(` and `)` or `(?:` and `)`. If `(` is
|
2017-10-13 08:42:58 +02:00
|
|
|
used, the group will be a capturing group, which means the positions from
|
2017-10-13 07:14:31 +02:00
|
|
|
the subject strings that matched between `(` and `)` will be recorded.
|
|
|
|
|
2019-01-03 12:52:15 +01:00
|
|
|
Capture groups are numbered starting at 1. They are numbered in the
|
|
|
|
order of appearance of their `(` in the regex. A special capture group
|
|
|
|
0 is for the whole sequence that matched.
|
2017-10-13 07:14:31 +02:00
|
|
|
|
2019-01-03 12:52:15 +01:00
|
|
|
* `(?:` introduces a non capturing group, which will not record the
|
2017-10-16 03:35:03 +02:00
|
|
|
matching positions.
|
2017-10-13 07:14:31 +02:00
|
|
|
|
2019-01-03 12:52:15 +01:00
|
|
|
* `(?<name>` introduces a named capturing group, which, in addition to
|
|
|
|
being referred by number, can be, in certain contexts, referred by the
|
|
|
|
given name.
|
|
|
|
|
2017-11-02 03:03:24 +01:00
|
|
|
== Alternations
|
2017-10-13 07:14:31 +02:00
|
|
|
|
2019-01-03 12:52:15 +01:00
|
|
|
The `|` character introduces an alternation, which will either match
|
|
|
|
its left-hand side, or its right-hand side (preferring the left-hand side)
|
2017-10-13 07:14:31 +02:00
|
|
|
|
|
|
|
For example, `foo|bar` matches either `foo` or `bar`, `foo(bar|baz|qux)`
|
|
|
|
matches `foo` followed by either `bar`, `baz` or `qux`.
|
|
|
|
|
2017-11-02 03:03:24 +01:00
|
|
|
== Quantifier
|
2017-10-13 07:14:31 +02:00
|
|
|
|
2021-05-10 08:40:01 +02:00
|
|
|
Literals, character classes, any characters, and groups can be followed
|
2017-10-13 07:14:31 +02:00
|
|
|
by a quantifier, which specifies the number of times they can match.
|
|
|
|
|
2021-05-10 08:40:01 +02:00
|
|
|
* `?` matches zero, or one time.
|
2017-10-13 07:14:31 +02:00
|
|
|
* `*` matches zero or more times.
|
|
|
|
* `+` matches one or more times.
|
2021-05-10 08:40:01 +02:00
|
|
|
* `{n}` matches exactly `n` times.
|
|
|
|
* `{n,}` matches `n` or more times.
|
|
|
|
* `{n,m}` matches `n` to `m` times.
|
|
|
|
* `{,m}` matches zero to `m` times.
|
2017-10-13 07:14:31 +02:00
|
|
|
|
|
|
|
By default, quantifiers are *greedy*, which means they will prefer to
|
|
|
|
match more characters if possible. Suffixing a quantifier with `?` will
|
2017-10-16 03:35:03 +02:00
|
|
|
make it non-greedy, meaning it will prefer to match as few characters
|
|
|
|
as possible.
|
2017-10-13 07:14:31 +02:00
|
|
|
|
2017-11-02 03:03:24 +01:00
|
|
|
== Zero width assertions
|
2017-10-13 07:14:31 +02:00
|
|
|
|
2021-05-10 08:40:01 +02:00
|
|
|
Assertions do not consume any character, but they will prevent the regex
|
|
|
|
from matching if not fulfilled.
|
2017-10-13 07:14:31 +02:00
|
|
|
|
2021-05-10 08:40:01 +02:00
|
|
|
* `^` matches at the start of a line; that is, just after a newline
|
|
|
|
character, or at the subject's beginning (unless it is specified
|
|
|
|
that the subject's beginning is not a start of line).
|
|
|
|
* `$` matches at the end of a line; that is, just before a newline, or
|
|
|
|
at the subject end (unless it is specified that the subject's end
|
2017-10-13 07:14:31 +02:00
|
|
|
is not an end of line).
|
2021-05-10 08:40:01 +02:00
|
|
|
* `\b` matches at a word boundary; which is to say that between the
|
|
|
|
previous character and the current character, one is a word
|
|
|
|
character, and the other is not.
|
|
|
|
* `\B` matches at a non-word boundary; meaning, when both the previous
|
|
|
|
character and the current character are word characters, or both
|
|
|
|
are not.
|
|
|
|
* `\A` matches at the subject string's beginning.
|
|
|
|
* `\z` matches at the subject string's end.
|
|
|
|
* `\K` matches anything, and resets the start position of capture group
|
|
|
|
0 to the current position.
|
2017-10-13 07:14:31 +02:00
|
|
|
|
|
|
|
More complex assertions can be expressed with lookarounds:
|
|
|
|
|
2021-05-10 08:40:01 +02:00
|
|
|
* `(?=...)` is a lookahead; it will match if its content matches the
|
|
|
|
text following the current position.
|
|
|
|
* `(?!...)` is a negative lookahead; it will match if its content does
|
|
|
|
not match the text following the current position.
|
|
|
|
* `(?<=...)` is a lookbehind; it will match if its content matches
|
|
|
|
the text preceding the current position.
|
|
|
|
* `(?<!...)` is a negative lookbehind; it will match if its content does
|
|
|
|
not match the text preceding the current position.
|
2017-10-13 07:14:31 +02:00
|
|
|
|
2021-05-10 08:40:01 +02:00
|
|
|
For performance reasons, lookaround contents must be a sequence of
|
|
|
|
literals, character classes, or any character (`.`); quantifiers are not
|
|
|
|
supported.
|
2017-10-13 07:14:31 +02:00
|
|
|
|
|
|
|
For example, `(?<!bar)(?=foo).` will match any character which is not
|
|
|
|
preceded by `bar` and where `foo` matches from the current position
|
|
|
|
(which means the character has to be an `f`).
|
|
|
|
|
2017-11-02 03:03:24 +01:00
|
|
|
== Modifiers
|
2017-10-13 07:14:31 +02:00
|
|
|
|
2017-10-13 08:42:58 +02:00
|
|
|
Some modifiers can control the matching behavior of the atoms following
|
2017-10-13 07:14:31 +02:00
|
|
|
them:
|
|
|
|
|
2021-05-10 08:40:01 +02:00
|
|
|
* `(?i)` starts case-insensitive matching.
|
|
|
|
* `(?I)` starts case-sensitive matching (default).
|
|
|
|
* `(?s)` allows `.` to match newlines (default).
|
|
|
|
* `(?S)` prevents `.` from matching newlines.
|
2017-10-13 07:14:31 +02:00
|
|
|
|
2017-11-02 03:03:24 +01:00
|
|
|
== Quoting
|
2017-10-13 07:14:31 +02:00
|
|
|
|
|
|
|
`\Q` will start a quoted sequence, where every character is treated as
|
|
|
|
a literal. That quoted sequence will continue until either the end of
|
|
|
|
the regex, or the appearance of `\E`.
|
|
|
|
|
2021-05-10 08:40:01 +02:00
|
|
|
For example, `.\Q.^$\E$` will match any character followed by the
|
|
|
|
literal string `.^$`, followed by an end of line.
|
2017-10-26 07:52:29 +02:00
|
|
|
|
2017-11-02 03:03:24 +01:00
|
|
|
== Compatibility
|
2017-10-26 07:52:29 +02:00
|
|
|
|
2021-05-10 08:40:01 +02:00
|
|
|
Kakoune's syntax tries to follow the ECMAScript regex syntax, as defined
|
|
|
|
by <https://www.ecma-international.org/ecma-262/8.0/>; some divergence
|
|
|
|
exists for ease of use, or performance reasons:
|
2017-10-26 07:52:29 +02:00
|
|
|
|
2021-05-10 08:40:01 +02:00
|
|
|
* Lookarounds are not arbitrary, but lookbehind is supported.
|
2017-10-26 07:52:29 +02:00
|
|
|
* `\K`, `\Q..\E`, `\A`, `\h` and `\z` are added.
|
2021-05-10 08:40:01 +02:00
|
|
|
* Stricter handling of escaping, as we introduce additional escapes;
|
|
|
|
identity escapes like `\X` with `X` being a non-special character
|
2017-10-26 07:52:29 +02:00
|
|
|
are not accepted, to avoid confusions between `\h` meaning literal
|
|
|
|
`h` in ECMAScript, and horizontal blank in Kakoune.
|
2021-05-10 08:40:01 +02:00
|
|
|
* `\uXXXXXX` uses 6 digits to cover all of Unicode, instead of relying
|
2019-11-06 10:48:48 +01:00
|
|
|
on ECMAScript UTF-16 surrogate pairs with 4 digits.
|