Improve this page
Quickly fork, edit online, and submit a pull request for this page.
Requires a signed-in GitHub account. This works well for small changes.
If you'd like to make larger changes you may want to consider using
local clone.
Page wiki
View or edit the community-maintained wiki page associated with this page.
std.regex
Intro
Regular expressions are a commonly used method of pattern matching on strings, with regex being a catchy word for a pattern in this domain specific language. Typical problems usually solved by regular expressions include validation of user input and the ubiquitous find & replace in text processing utilities.Synopsis
import std.regex; import std.stdio; void main() { // Print out all possible dd/mm/yy(yy) dates found in user input. // g - global: find all matches. auto r = regex(r"\b[0-9][0-9]?/[0-9][0-9]?/[0-9][0-9](?:[0-9][0-9])?\b", "g"); foreach(line; stdin.byLine) { // Match returns a range that can be iterated // to get all subsequent matches. foreach(c; match(line, r)) writeln(c.hit); } } ... // Create a static regex at compile-time, which contains fast native code. auto ctr = ctRegex!(`^.*/([^/]+)/?$`); // It works just like a normal regex: auto m2 = match("foo/bar", ctr); // First match found here, if any assert(m2); // Be sure to check if there is a match before examining contents! assert(m2.captures[1] == "bar"); // Captures is a range of submatches: 0 = full match. ... // The result of the match is directly testable with if/assert/while. // e.g. test if a string consists of letters: assert(match("Letter", `^\p{L}+$`));
Syntax and general information
The general usage guideline is to keep regex complexity on the side of simplicity, as its capabilities reside in purely character-level manipulation. As such it's ill-suited for tasks involving higher level invariants like matching an integer number bounded in an [a,b] interval. Checks of this sort of are better addressed by additional post-processing. The basic syntax shouldn't surprise experienced users of regular expressions. For an introduction to std.regex see a short tour of the module API and its abilities. There are other web resources on regular expressions to help newcomers, and a good reference with tutorial can easily be found. This library uses a remarkably common ECMAScript syntax flavor with the following extensions:- Named subexpressions, with Python syntax.
- Unicode properties such as Scripts, Blocks and common binary properties e.g Alphabetic, White_Space, Hex_Digit etc.
- Arbitrary length and complexity lookbehind, including lookahead in lookbehind and vise-versa.
Pattern syntax
std.regex operates on codepoint level,
'character' in this table denotes a single Unicode codepoint.
Character classes
Pattern element | Semantics |
Any atom | Has the same meaning as outside of a character class. |
a-z | Includes characters a, b, c, ..., z. |
[a||b], [a--b], [a~~b], [a&&b] | Where a, b are arbitrary classes, means union, set difference, symmetric set difference, and intersection respectively. Any sequence of character class elements implicitly forms a union. |
Regex flags
Unicode support
This library provides full Level 1 support* according to UTS 18. Specifically:- 1.1 Hex notation via any of \uxxxx, \U00YYYYYY, \xZZ.
- 1.2 Unicode properties.
- 1.3 Character classes with set operations.
- 1.4 Word boundaries use the full set of "word" characters.
- 1.5 Using simple casefolding to match case insensitively across the full range of codepoints.
- 1.6 Respecting line breaks as any of \u000A | \u000B | \u000C | \u000D | \u0085 | \u2028 | \u2029 | \u000D\u000A.
- 1.7 Operating on codepoint level.
Replace format string
A set of functions in this module that do the substitution rely on a simple format to guide the process. In particular the table below applies to the format argument of replaceFirst and replaceAll. The format string can reference parts of match using the following notation.Format specifier | Replaced by |
$& | the whole match. |
$` | part of input preceding the match. |
$' | part of input following the match. |
$$ | '$' character. |
\c , where c is any character | the character c itself. |
\\ | '\' character. |
$1 .. $99 | submatch number 1 to 99 respectively. |
Slicing and zero memory allocations orientation
All matches returned by pattern matching functionality in this library are slices of the original input. The notable exception is the replace family of functions that generate a new string from the input. In cases where producing the replacement is the ultimate goal replaceFirstInto and replaceAllInto could come in handy as functions that avoid allocations even for replacement. License:Boost License 1.0. Authors:
Dmitry Olshansky, API and utility constructs are modeled after the original std.regex by Walter Bright and Andrei Alexandrescu. Source:
std/regex.d