Import std.text.regex
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)-
The pattern, as it was given.
-
The flags it was read with.
-
A Regex of
pattern, read asflagssays. Panics when it isn't a validpattern, with PCRE2's message and where in thepatternit is. -
Public Static Method Parse(pattern: String, flags: RegexFlags = RegexFlags.NONE): Regex Throws RegexErrorstatic method #A Regex of
pattern, read asflagssays, for a pattern from elsewhere. Throws a RegexError of PATTERN when it isn't a validpattern, whose Offset is where in thepatternit is. -
textwith a backslash before each character that can mean something in a pattern, so a Regex of it matchestextitself, 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. -
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.
-
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.
-
How many groups the pattern has, not counting the whole match, which is group 0.
-
The names of the pattern's groups, sorted.
-
Whether the pattern matches somewhere in
text. -
The first match at
startor after, or Null. Panics unlessstartis a position intext, between its characters. -
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 RegexErrormethod #A copy of
textwith each match replaced byreplacement, in which $1 or ${1} is group 1'stext, $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 RegexErrormethod #A copy of
textwith each match replaced by whatreplacementgives for it. -
@PanicsAtCaller Public Method ReplaceFirst(text: String, replacement: String): String Throws RegexErrormethod #A copy of
textwith its first match replaced byreplacement, as Replace does each. -
Public Method ReplaceFirst(text: String, replacement: Func(Match): String): String Throws RegexErrormethod #A copy of
textwith its first match replaced by whatreplacementgives for it. -
The parts of
textbetween 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.