Skip to content
FutureBlitz

    Install

    Import std.text.regex

    Regex

    std.text.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.

    Examples

    Local date = Regex(r"(\d{4})-(\d{2})-(\d{2})")
    Local found = Try date.Find("landed 1969-07-20, at night")
    Expect(found?.Value == "1969-07-20" && found?.Start == 7)
    Expect((Try date.Replace("1969-07-20", "$3/$2/$1")) == "20/07/1969")
    Expect((Try date.FindAll("1969-07-20 and 1972-12-14")).Count == 2)

    std/text/regex.fb, line 222

    • 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.

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