regex

Compile regular expression pattern for the later execution.

  1. auto regex(S[] patterns, const(char)[] flags)
  2. auto regex(S pattern, const(char)[] flags)
    @trusted
    regex
    (
    S
    )
    (,
    const(char)[] flags = ""
    )

Parameters

pattern S

A single regular expression to match.

flags const(char)[]

The _attributes (g, i, m, s and x accepted)

Return Value

Type: auto

Regex object that works on inputs having the same character width as pattern.

Throws

RegexException if there were any errors during compilation.

Examples

1 void test(S)()
2 {
3     // multi-pattern regex example
4     S[] arr = [`([a-z]+):(\d+)`, `(\d+),\d+`];
5     auto multi = regex(arr); // multi regex
6     S str = "abc:43 12,34";
7     auto m = str.matchAll(multi);
8     assert(m.front.whichPattern == 1);
9     assert(m.front[1] == "abc");
10     assert(m.front[2] == "43");
11     m.popFront();
12     assert(m.front.whichPattern == 2);
13     assert(m.front[1] == "12");
14 }
15 
16 import std.meta : AliasSeq;
17 static foreach (C; AliasSeq!(string, wstring, dstring))
18     // Test with const array of patterns - see https://issues.dlang.org/show_bug.cgi?id=20301
19     static foreach (S; AliasSeq!(C, const C, immutable C))
20         test!S();

Meta

Suggestion Box / Bug Report