Skip to content
FutureBlitz

    Install

    Import std.text.regex

    std.text.regex

    std.text.regex: patterns over PCRE2, through the runtime's fb_regex_ functions. A pattern is Perl's syntax as PCRE2 has it, over UTF-8: \w, \d, \s, and \b take Unicode's letters, digits, and spaces, and positions are byte offsets, as a String's are. Each search for a match has limits, its Regex's MatchLimit and MemoryLimit, past which it throws a RegexError instead of running on, as a pattern that backtracks without end would. A pattern that isn't valid panics where its Regex is made, and the compiler reports a literal one at it; Regex.Parse throws instead, for a pattern from elsewhere, like a program's user.

    std/text/regex.fb

    Types

    RegexFlags enum

    @Flags Public Enum RegexFlags

    How a Regex reads its pattern.

    std/text/regex.fb, line 33

    • NONE = 0 enum member #

      As it's written.

    • IGNORE_CASE enum member #

      Letters match in either case.

    • MULTILINE enum member #

      ^ and $ match at each line's start and end too.

    • DOT_ALL enum member #

      . matches a line break.

    • EXTENDED enum member #

      The pattern's whitespace is skipped, and its comments from # to the line's end.

    RegexErrorKind enum

    Public Enum RegexErrorKind

    What kind of RegexError it is.

    std/text/regex.fb, line 48

    • PATTERN enum member #

      A pattern that isn't valid.

    • MATCH_LIMIT enum member #

      A match that went past its Regex's MatchLimit.

    • MEMORY_LIMIT enum member #

      A match that went past its Regex's MemoryLimit.

    RegexError class

    Public Class RegexError Extends Error

    A pattern that isn't valid, at the byte Offset of the pattern, or a match that went past a limit, where Offset is where in the text it started looking.

    std/text/regex.fb, line 59

    • Public ReadOnly Field Kind: RegexErrorKind field #

      What kind of RegexError it is.

    • Public ReadOnly Field Offset: Int field #

      The byte offset in the pattern of what isn't valid, or in the text of where the match started looking.

    • Public New(kind: RegexErrorKind, message: String, offset: Int, cause: Error? = Null) constructor #

      A RegexError of kind that says message, at offset.

    Match class

    Public Class Match Implements Printable

    One match of a Regex in Text: where it starts and finishes, and its groups' text, group 0 being the whole match.

    std/text/regex.fb, line 76

    • Public ReadOnly Field Text: String field #

      The text the match is in.

    • Public Property Start: Int property #

      Where it starts.

    • Public Property Finish: Int property #

      Where it finishes, just past its last byte.

    • Public Property Value: String property #

      The text it matched.

    • Public Property GroupCount: Int property #

      How many groups the pattern has, not counting the whole match.

    • @PanicsAtCaller Operator [](number: Int): String? operator #

      Group number's text, the whole match's for 0, or Null when the group took no part. Panics unless the pattern has that group.

    • @PanicsAtCaller Operator [](name: String): String? operator #

      The text of the group called name, or Null when it took no part. Panics unless the pattern has a group of that name. Where several have it, it's the first that took part.

    • @PanicsAtCaller Public Method StartOf(number: Int): Int? method #

      Where group number starts, or Null when it took no part. Panics unless the pattern has that group.

    • @PanicsAtCaller Public Method StartOf(name: String): Int? method #

      Where the group called name starts, or Null when it took no part. Panics unless the pattern has a group of that name.

    • @PanicsAtCaller Public Method FinishOf(number: Int): Int? method #

      Where group number finishes, or Null when it took no part. Panics unless the pattern has that group.

    • @PanicsAtCaller Public Method FinishOf(name: String): Int? method #

      Where the group called name finishes, or Null when it took no part. Panics unless the pattern has a group of that name.

    • Public Method ToString(): String method #

      Its text, as a string interpolation like "{value}" shows it.

      Doc from std.core.interfaces.Printable.ToString.

    Regex class

    Public Class Regex

    A compiled pattern, which finds its matches in text. Regex("[0-9]+") is one, and the compiler checks a literal pattern there.

    std/text/regex.fb, line 191

    • Public ReadOnly Field Pattern: String field #

      The pattern, as it was given.

    • Public ReadOnly Field Flags: RegexFlags field #

      The flags it was read with.

    • @PanicsAtCaller Public New(pattern: String, flags: RegexFlags = RegexFlags.NONE) constructor #

      A Regex of pattern, read as flags says. Panics when it isn't a valid pattern, with PCRE2's message and where in the pattern it is.

    • Public Static Method Parse(pattern: String, flags: RegexFlags = RegexFlags.NONE): Regex Throws RegexError static method #

      A Regex of pattern, read as flags says, for a pattern from elsewhere. Throws a RegexError of PATTERN when it isn't a valid pattern, whose Offset is where in the pattern it is.

    • Public Static Method Escape(text: String): String static method #

      text with a backslash before each character that can mean something in a pattern, so a Regex of it matches text itself, under any flags: each ASCII one but a letter, a digit, or _, and the whitespace beyond ASCII that EXTENDED skips, U+0085, U+200E, U+200F, U+2028, and U+2029.

    • @PanicsAtCaller Public Property MatchLimit: Int property #

      The most steps a search for one match takes, over every place in the text it tries, 10 million at first, past which it throws a RegexError of MATCH_LIMIT. A step is an item of the pattern that PCRE2's matching tries, and a pattern that backtracks without end takes ever more. Panics when set below 1.

    • @PanicsAtCaller Public Property MemoryLimit: Int property #

      The most memory one match takes, in bytes, 64 MiB at first, past which it throws a RegexError of MEMORY_LIMIT. Panics when set below 1 MiB, since PCRE2 starts each match with tens of KiB.

    • Public Property GroupCount: Int property #

      How many groups the pattern has, not counting the whole match, which is group 0.

    • Public Property GroupNames: String[] property #

      The names of the pattern's groups, sorted.

    • Public Method IsMatch(text: String): Bool Throws RegexError method #

      Whether the pattern matches somewhere in text.

    • @PanicsAtCaller Public Method Find(text: String, start: Int = 0): Match? Throws RegexError method #

      The first match at start or after, or Null. Panics unless start is a position in text, between its characters.

    • Public Method FindAll(text: String): Match[] Throws RegexError method #

      Each match, from the start: after one, the next is looked for from its end, and after an empty one, from the next character unless a match that isn't empty starts where it is.

    • @PanicsAtCaller Public Method Replace(text: String, replacement: String): String Throws RegexError method #

      A copy of text with each match replaced by replacement, in which $1 or ${1} is group 1's text, $0 the whole match's, ${name} the group called name's, and $$ a $. A group that took no part is "". Panics on a $ before anything else, or a group the pattern doesn't have.

    • Public Method Replace(text: String, replacement: Func(Match): String): String Throws RegexError method #

      A copy of text with each match replaced by what replacement gives for it.

    • @PanicsAtCaller Public Method ReplaceFirst(text: String, replacement: String): String Throws RegexError method #

      A copy of text with its first match replaced by replacement, as Replace does each.

    • Public Method ReplaceFirst(text: String, replacement: Func(Match): String): String Throws RegexError method #

      A copy of text with its first match replaced by what replacement gives for it.

    • Public Method Split(text: String): String[] Throws RegexError method #

      The parts of text between its matches: "a1b22c" split by "[0-9]+" is "a", "b", and "c". A match at the start or the end makes an empty part there.

    Extends

    String extend

    Extend String

    No doc comment.

    std/text/regex.fb, line 510

    • @PanicsAtCaller Public Method Matches(pattern: String): Bool Throws RegexError method #

      Whether pattern matches somewhere in it, as Regex(pattern).IsMatch does. Its compiled pattern is kept for a while, so calling it again with the same one doesn't compile it again.

    • @PanicsAtCaller Public Method ReplaceAll(pattern: String, replacement: String): String Throws RegexError method #

      A copy with each match of pattern replaced by replacement, as Regex(pattern).Replace does.

    • @PanicsAtCaller Public Method SplitBy(pattern: String): String[] Throws RegexError method #

      The parts between each match of pattern, as Regex(pattern).Split gives them.

    Report an issue with this page or with FutureBlitz on GitHub.