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

void test(S)()
{
    // multi-pattern regex example
    S[] arr = [`([a-z]+):(\d+)`, `(\d+),\d+`];
    auto multi = regex(arr); // multi regex
    S str = "abc:43 12,34";
    auto m = str.matchAll(multi);
    assert(m.front.whichPattern == 1);
    assert(m.front[1] == "abc");
    assert(m.front[2] == "43");
    m.popFront();
    assert(m.front.whichPattern == 2);
    assert(m.front[1] == "12");
}

import std.meta : AliasSeq;
static foreach (C; AliasSeq!(string, wstring, dstring))
    // Test with const array of patterns - see https://issues.dlang.org/show_bug.cgi?id=20301
    static foreach (S; AliasSeq!(C, const C, immutable C))
        test!S();

Meta