Standard Library
class RegexException
The exception thrown for a malformed pattern, and for a malformed or out-of-range reference in a replacement string.
since 0.1.0-alpha.1linuxwindowswasm
- bases
Exception
Overview
message describes the problem. offset is the byte position in the pattern (or in the replacement string) where the problem was found, or -1 when that is not known. It is thrown by every call that takes its pattern as code: the Regex constructors and the regex:: functions. To handle a pattern that comes from data without an exception, use regex::compile, which returns None instead.
Examples
Catching a bad pattern
try {
Regex bad = Regex("a(b");
console.writeln("unreachable");
} catch (RegexException e) {
console.writeln(e.message);
console.writeln(e.offset);
}
regex: unterminated group at offset 3
3
Constructors
new
new(string msg, int off)Create the exception.
Parameters
- msg
- The message.
- off
- The byte offset of the problem, or
-1.
Examples
Throwing it by hand
try {
throw RegexException("custom failure", 7);
} catch (RegexException e) {
console.writeln("${e.message} at ${e.offset}");
}
custom failure at 7
Fields
offset
int offsetByte position of the problem in the pattern or replacement string, or -1 when unknown.
See also
- compile — Compile a pattern that comes from data, without throwing.