Skip to content

Instantly share code, notes, and snippets.

@Nine5102
Created January 8, 2013 01:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nine5102/5a0b4685c6d316620c86 to your computer and use it in GitHub Desktop.
Save Nine5102/5a0b4685c6d316620c86 to your computer and use it in GitHub Desktop.
//Code:
import std.regex;
import std.stdio : writefln;
int main(string[] args)
{
string tomatch = "a!b@c";
version(Static)
{
static auto r = regex(r"^(?P<nick>.*?)!(?P<ident>.*?)@(?P<host>.*?)$");
} else {
auto r = regex(r"^(?P<nick>.*?)!(?P<ident>.*?)@(?P<host>.*?)$");
}
auto nm = match(tomatch, r);
assert(nm);
auto c = nm.captures;
writefln("c[1] = %s", c[1]);
writefln("c[\"nick\"] = %s", c["nick"]);
return 0;
}
/* Results:
* $ dmd -run regex.d
* c[1] = a
* c["nick"] = a
* $ dmd -version=Static -run regex.d
* c[1] = a
* core.exception.RangeError@std.regex(2145): Range violation
* ----------------
* regex(_d_array_bounds+0x16) [0x80c9746]
* regex() [0x80ce916]
* regex(@trusted uint std.regex.lookupNamedGroup!(immutable(char)[]).lookupNamedGroup(std.regex.NamedGroup[], immutable(char)[])+0x5a) [0x80c5002]
* regex(@trusted immutable(char)[] std.regex.Captures!(immutable(char)[], uint).Captures.opIndex!(immutable(char)[]).opIndex(immutable(char)[])+0x48) [0x80c4f20]
* regex(_Dmain+0xa5) [0x809ef79]
* regex(extern (C) int rt.dmain2.main(int, char**).void runMain()+0x14) [0x80c9d20]
* regex(extern (C) int rt.dmain2.main(int, char**).void tryExec(scope void delegate())+0x18) [0x80c9830]
* regex(extern (C) int rt.dmain2.main(int, char**).void runAll()+0x32) [0x80c9d62]
* regex(extern (C) int rt.dmain2.main(int, char**).void tryExec(scope void delegate())+0x18) [0x80c9830]
* regex(main+0x94) [0x80c97e4]
* /lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3) [0xb75c94d3]
* ----------------
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment