| 1 |
@system unittest |
| 2 |
{ |
| 3 |
import std.regex; |
| 4 |
|
| 5 |
void test(S)() |
| 6 |
{ |
| 7 |
// multi-pattern regex example |
| 8 |
S[] arr = [`([a-z]+):(\d+)`, `(\d+),\d+`]; |
| 9 |
auto multi = regex(arr); // multi regex |
| 10 |
S str = "abc:43 12,34"; |
| 11 |
auto m = str.matchAll(multi); |
| 12 |
assert(m.front.whichPattern == 1); |
| 13 |
assert(m.front[1] == "abc"); |
| 14 |
assert(m.front[2] == "43"); |
| 15 |
m.popFront(); |
| 16 |
assert(m.front.whichPattern == 2); |
| 17 |
assert(m.front[1] == "12"); |
| 18 |
} |
| 19 |
|
| 20 |
import std.meta : AliasSeq; |
| 21 |
static foreach (C; AliasSeq!(string, wstring, dstring)) |
| 22 |
// Test with const array of patterns - see https://issues.dlang.org/show_bug.cgi?id=20301 |
| 23 |
static foreach (S; AliasSeq!(C, const C, immutable C)) |
| 24 |
test!S(); |
| 25 |
} |
| 26 |
|
| 27 |
@system unittest |
| 28 |
{ |
| 29 |
import std.regex; |
| 30 |
|
| 31 |
import std.regex; |
| 32 |
assert(matchFirst("abc", "[0-9]+", "[a-z]+").whichPattern == 2); |
| 33 |
|
| 34 |
} |
| 35 |
|
| 36 |
@system unittest |
| 37 |
{ |
| 38 |
import std.regex; |
| 39 |
|
| 40 |
import std.range.primitives : popFrontN; |
| 41 |
|
| 42 |
auto c = matchFirst("@abc#", regex(`(\w)(\w)(\w)`)); |
| 43 |
assert(c.pre == "@"); // Part of input preceding match |
| 44 |
assert(c.post == "#"); // Immediately after match |
| 45 |
assert(c.hit == c[0] && c.hit == "abc"); // The whole match |
| 46 |
assert(c[2] == "b"); |
| 47 |
assert(c.front == "abc"); |
| 48 |
c.popFront(); |
| 49 |
assert(c.front == "a"); |
| 50 |
assert(c.back == "c"); |
| 51 |
c.popBack(); |
| 52 |
assert(c.back == "b"); |
| 53 |
popFrontN(c, 2); |
| 54 |
assert(c.empty); |
| 55 |
|
| 56 |
assert(!matchFirst("nothing", "something")); |
| 57 |
|
| 58 |
// Captures that are not matched will be null. |
| 59 |
c = matchFirst("ac", regex(`a(b)?c`)); |
| 60 |
assert(c); |
| 61 |
assert(!c[1]); |
| 62 |
} |
| 63 |
|
| 64 |
@system unittest |
| 65 |
{ |
| 66 |
import std.regex; |
| 67 |
|
| 68 |
assert(replaceFirst("noon", regex("n"), "[$&]") == "[n]oon"); |
| 69 |
} |
| 70 |
|
| 71 |
@system unittest |
| 72 |
{ |
| 73 |
import std.regex; |
| 74 |
|
| 75 |
import std.conv : to; |
| 76 |
string list = "#21 out of 46"; |
| 77 |
string newList = replaceFirst!(cap => to!string(to!int(cap.hit)+1)) |
| 78 |
(list, regex(`[0-9]+`)); |
| 79 |
assert(newList == "#22 out of 46"); |
| 80 |
} |
| 81 |
|
| 82 |
@system unittest |
| 83 |
{ |
| 84 |
import std.regex; |
| 85 |
|
| 86 |
import std.array; |
| 87 |
string m1 = "first message\n"; |
| 88 |
string m2 = "second message\n"; |
| 89 |
auto result = appender!string(); |
| 90 |
replaceFirstInto(result, m1, regex(`([a-z]+) message`), "$1"); |
| 91 |
//equivalent of the above with user-defined callback |
| 92 |
replaceFirstInto!(cap=>cap[1])(result, m2, regex(`([a-z]+) message`)); |
| 93 |
assert(result.data == "first\nsecond\n"); |
| 94 |
} |
| 95 |
|
| 96 |
@system unittest |
| 97 |
{ |
| 98 |
import std.regex; |
| 99 |
|
| 100 |
// insert comma as thousands delimiter |
| 101 |
auto re = regex(r"(?<=\d)(?=(\d\d\d)+\b)","g"); |
| 102 |
assert(replaceAll("12000 + 42100 = 54100", re, ",") == "12,000 + 42,100 = 54,100"); |
| 103 |
} |
| 104 |
|
| 105 |
@system unittest |
| 106 |
{ |
| 107 |
import std.regex; |
| 108 |
|
| 109 |
string baz(Captures!(string) m) |
| 110 |
{ |
| 111 |
import std.string : toUpper; |
| 112 |
return toUpper(m.hit); |
| 113 |
} |
| 114 |
// Capitalize the letters 'a' and 'r': |
| 115 |
auto s = replaceAll!(baz)("Strap a rocket engine on a chicken.", |
| 116 |
regex("[ar]")); |
| 117 |
assert(s == "StRAp A Rocket engine on A chicken."); |
| 118 |
} |
| 119 |
|
| 120 |
@system unittest |
| 121 |
{ |
| 122 |
import std.regex; |
| 123 |
|
| 124 |
// insert comma as thousands delimiter in fifty randomly produced big numbers |
| 125 |
import std.array, std.conv, std.random, std.range; |
| 126 |
static re = regex(`(?<=\d)(?=(\d\d\d)+\b)`, "g"); |
| 127 |
auto sink = appender!(char [])(); |
| 128 |
enum ulong min = 10UL ^^ 10, max = 10UL ^^ 19; |
| 129 |
foreach (i; 0 .. 50) |
| 130 |
{ |
| 131 |
sink.clear(); |
| 132 |
replaceAllInto(sink, text(uniform(min, max)), re, ","); |
| 133 |
foreach (pos; iota(sink.data.length - 4, 0, -4)) |
| 134 |
assert(sink.data[pos] == ','); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
@system unittest |
| 139 |
{ |
| 140 |
import std.regex; |
| 141 |
|
| 142 |
import std.algorithm.comparison : equal; |
| 143 |
auto s1 = ", abc, de, fg, hi, "; |
| 144 |
assert(equal(splitter(s1, regex(", *")), |
| 145 |
["", "abc", "de", "fg", "hi", ""])); |
| 146 |
} |
| 147 |
|
| 148 |
@system unittest |
| 149 |
{ |
| 150 |
import std.regex; |
| 151 |
|
| 152 |
import std.algorithm.comparison : equal; |
| 153 |
import std.typecons : Yes; |
| 154 |
|
| 155 |
auto pattern = regex(`([\.,])`); |
| 156 |
|
| 157 |
assert("2003.04.05" |
| 158 |
.splitter!(Yes.keepSeparators)(pattern) |
| 159 |
.equal(["2003", ".", "04", ".", "05"])); |
| 160 |
|
| 161 |
assert(",1,2,3" |
| 162 |
.splitter!(Yes.keepSeparators)(pattern) |
| 163 |
.equal([",", "1", ",", "2", ",", "3"])); |
| 164 |
} |
| 165 |
|
| 166 |
@system unittest |
| 167 |
{ |
| 168 |
import std.regex; |
| 169 |
|
| 170 |
import std.algorithm.comparison; |
| 171 |
import std.regex; |
| 172 |
string s = `This is {unfriendly} to *regex*`; |
| 173 |
assert(s.escaper.equal(`This is \{unfriendly\} to \*regex\*`)); |
| 174 |
} |
| 175 |
|