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.
-
As it's written.
-
Letters match in either case.
-
^ and $ match at each line's start and end too.
-
. matches a line break.
-
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.
-
A pattern that isn't valid.
-
A match that went past its Regex's MatchLimit.
-
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.
-
What kind of RegexError it is.
-
The byte offset in the pattern of what isn't valid, or in the text of where the match started looking.
-
A RegexError of
kindthat saysmessage, atoffset.
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.
-
The text the match is in.
-
Where it starts.
-
Where it finishes, just past its last byte.
-
The text it matched.
-
How many groups the pattern has, not counting the whole match.
-
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.
-
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.
-
Where group
numberstarts, or Null when it took no part. Panics unless the pattern has that group. -
Where the group called
namestarts, or Null when it took no part. Panics unless the pattern has a group of thatname. -
Where group
numberfinishes, or Null when it took no part. Panics unless the pattern has that group. -
Where the group called
namefinishes, or Null when it took no part. Panics unless the pattern has a group of thatname. -
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.
-
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.
Extends
String
extend
Extend String
No doc comment.
-
Whether
patternmatches somewhere in it, as Regex(pattern).IsMatch does. Its compiledpatternis 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 RegexErrormethod #A copy with each match of
patternreplaced byreplacement, as Regex(pattern).Replace does. -
The parts between each match of
pattern, as Regex(pattern).Split gives them.
Report an issue with this page or with FutureBlitz on GitHub.