6.93. kink/regex/REGEX

The mod provides a regular expression engine backed by java.util.regex package.

The syntax of regex patterns is same as one of Java. See:

https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/regex/Pattern.html

There are three basic types.

• regex: A regular expression pattern. A regex val provides features such as matching, searching, replacing and splitting.

• match: A result of a successful matching. A regex_match contains groups. See kink/regex/MATCH for details.

• group: A slice in the matched text, which can be the entire slice of the regex_match, or a slice of a named capturing group. See kink/regex/GROUP for details.

`match` type is a subtype of `group`, which represents the entire slice of the matched area.

Regexes handle indices of code points, in contrast to java.util.regex which handles indices of UTF-16 code units.

Example

6.93.1. type regex

`regex` is a type of immutable regular expression pattern.

`regex` provides features such as matching, searching, replacing and splitting.

6.93.1.1. Regex.pattern

`pattern` returns the pattern str from which the regex is made.

== Example =a=

6.93.1.2. Regex.accept?(Text)

`accept?` returns whether the entire `Text` str matches the pattern of the regex

Precondition:

• `Text` must be a str.

Example

6.93.1.3. Regex.match(Text ...[$config={}])

`match` tries to match the regex to the entire `Text` str.

Config methods:

• C.on_success($success): default = VAL.identity.

• C.on_error($error): default = a fun which raises an exception.

If matched, `match` tail-calls $success with a val of `match` type.

If not matched, `match` tail-calls $error with no arg.

Preconditions

• `Text` must be a str.

• $success must be a fun which takes a `match` val.

• $error must be a fun which takes no arg.

Example

Example

6.93.1.4. Regex.search(Text Start_pos ...[$config={}])

`search` searches for the first slice of the `Text` which the regex matches. The search starts from `Start_pos`.

Config methods:

• C.on_success($success): default = VAL.identity

• C.on_error($error): default = a fun which raises an exception

If the search succeeds, `search` tail-calls $success with a `match` val.

If the search fails, `search` tail-calls $error with no arg.

Preconditions

• `Text` must be a str

• `Start_pos` must be an int num in the range [0, Text.size]

• $success must be a fun which takes a `match` val

• $error must be a fun which takes no arg

Example

Example

6.93.1.5. Regex.search_all(Text)

`search_all` searches for all the slices of the `Text` which the regex matches, and returns an iter of `match` vals for the found slices.

The first search is attempted from the beginning of the Text.

If a search from `Ind` results in a `match` for an empty slice, the next search is attempted from (Ind + 1). Otherwise, the next search is attempted from `Match.to` of the current `match`.

Precondition

• `Text` must be a str.

Example

6.93.1.6. Regex.replace_all(Text $match_to_str)

`replace_all` replaces all the slices of the `Text` which the regex matches.

Searching is done in the way same as `search_all`.

Matched slices are replaced by $match_to_str. $match_to_str must take a `match`, and return a str.

Precondition

• `Text` must be a str.

Example

Convert hex literals to decimal:

6.93.1.7. Regex.split(Text ...[Max_field_count])

`split` splits `Text` into a vec of str vals, using the regex as the pattern of delimiters.

If `Max_field_count` is specified, spliting is performed at most `Max_field_count - 1` times. Thus, the number of fields is no more than `Max_field_count`.

If `Max_field_count` is not specified, splitting is performed on all the matches.

Precondition

• `Max_field_count` must be an int num greater than or equal to 1

Example

6.93.2. REGEX.compile(Pattern ...[$config])

`compile` makes a regex from `Pattern`.

Config methods:

• C.on_success($success): default = VAL.identity.

• C.on_error($error): default = a fun which raises an exception.

If `Pattern` can be compiled as a regex, `compile` tail-calls $success with the created regex val.

If compilation hits a syntax error, `compile` tail-calls $error with (Error_msg, Ind), where `Error_msg` is a str of the error message str, and `Ind` is an int num of the index of the error place in `Pattern`.

Preconditions

• Pattern must be a str.

• $config must be a fun which takes a conf val.

• $success must be a fun which takes a `regex` val.

• $error must be a fun which takes (str, int num).

Example

Example

6.93.3. REGEX.is?(Val)

`is?` returns whether the `Val` is a regex val.

6.93.4. REGEX.escape(Str)

`escape` escapes regex special characters in `Str`. The result can be used as an arg of REGEX.compile, or can be embedded in a regex pattern.

Precondition:

• `Str` must be a str.

Example: