View source code
Display the source code in std/regex.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
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.

std.regex.Splitter/splitter - multiple declarations

Function splitter

Splits a string r using a regular expression pat as a separator.

Splitter!(keepSeparators,Range,RegEx) splitter(Flag!("keepSeparators") keepSeparators = No.keepSeparators, Range, RegEx) (
  Range r,
  RegEx pat
)
if (is(BasicElementOf!Range : dchar) && isRegexFor!(RegEx, Range));

Parameters

NameDescription
keepSeparators flag to specify if the matches should be in the resulting range
r the string to split
pat the pattern to split on

Returns

A lazy range of strings

Example

import std.algorithm.comparison : equal;
auto s1 = ", abc, de,  fg, hi, ";
assert(equal(splitter(s1, regex(", *")),
    ["", "abc", "de", "fg", "hi", ""]));

Example

Split on a pattern, but keep the matches in the resulting range

import std.algorithm.comparison : equal;
import std.typecons : Yes;

auto pattern = regex(`([\.,])`);

assert("2003.04.05"
    .splitter!(Yes.keepSeparators)(pattern)
    .equal(["2003", ".", "04", ".", "05"]));

assert(",1,2,3"
    .splitter!(Yes.keepSeparators)(pattern)
    .equal([",", "1", ",", "2", ",", "3"]));

Struct Splitter

Splits a string r using a regular expression pat as a separator.

struct Splitter(Flag!("keepSeparators") keepSeparators = No.keepSeparators, Range, alias RegEx)
  
if (isSomeString!Range && isRegexFor!(RegEx, Range));

Properties

NameTypeDescription
empty[get] boolForward range primitives.
front[get] RangeForward range primitives.
save[get] autoForward range primitives.

Methods

NameDescription
popFront () Forward range primitives.

Parameters

NameDescription
keepSeparators flag to specify if the matches should be in the resulting range
r the string to split
pat the pattern to split on

Returns

A lazy range of strings

Example

import std.algorithm.comparison : equal;
auto s1 = ", abc, de,  fg, hi, ";
assert(equal(splitter(s1, regex(", *")),
    ["", "abc", "de", "fg", "hi", ""]));

Example

Split on a pattern, but keep the matches in the resulting range

import std.algorithm.comparison : equal;
import std.typecons : Yes;

auto pattern = regex(`([\.,])`);

assert("2003.04.05"
    .splitter!(Yes.keepSeparators)(pattern)
    .equal(["2003", ".", "04", ".", "05"]));

assert(",1,2,3"
    .splitter!(Yes.keepSeparators)(pattern)
    .equal([",", "1", ",", "2", ",", "3"]));

Authors

Dmitry Olshansky,

API and utility constructs are modeled after the original std.regex by Walter Bright and Andrei Alexandrescu.

License

Boost License 1.0.