Why doesn't this work...Struct std.regex.Captures

WhatMeForget via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jul 8 23:30:34 PDT 2017


I was playing around with regex

import std.regex;
import std.stdio;

void main()
{
     string[] fileNames = [ "011_bad.txt", "01_01_pass", 
"90_pass", "_bad" ];

     auto reg = regex(r"^\d{2}_");

     foreach (name; fileNames)
     {
         auto c = matchFirst(name, reg);

         //writeln(c.hit);  // Slice of matched portion of input.
         if (c.empty)
             writeln(name, " is not the form we are looking for");
         else
             writeln(name, " is a winner");
     }
}

All is good:
011_bad.txt is not the form we are looking for
01_01_pass is a winner
90_pass is a winner
_bad is not the form we are looking for


but then I uncommented the writeln(c.hit); above and all hell 
breaks loose:


core.exception.AssertError@/usr/include/dmd/phobos/std/regex/package.d(559): Assertion failure
----------------
??:? _d_assertp [0x8124936]
??:? pure nothrow @property @nogc @trusted immutable(char)[] 
std.regex.Captures!(immutable(char)[], uint).Captures.hit() 
[0x81190d6]
??:? _Dmain [0x80ff351]
??:? 
_D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFNlZv 
[0x8125bbe]
??:? scope void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).tryExec(scope void delegate()) [0x8125b10]
??:? scope void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).runAll() [0x8125b7e]
??:? scope void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).tryExec(scope void delegate()) [0x8125b10]
??:? _d_run_main [0x8125aa8]
??:? main [0x8122ff3]
??:? __libc_start_main [0xb74dc275]



I got inspiration from the following "live" code at at 
.../captures.html. I inserted a writeln(c.hit) and here it worked!

import std.range.primitives : popFrontN;

auto c = matchFirst("@abc#", regex(`(\w)(\w)(\w)`));
assert(c.pre == "@"); // Part of input preceding match
assert(c.post == "#"); // Immediately after match
assert(c.hit == c[0] && c.hit == "abc"); // The whole match
writeln("My Addition ",c.hit);
writeln(c[2]); // "b"
writeln(c.front); // "abc"
c.popFront();
writeln(c.front); // "a"
writeln(c.back); // "c"
c.popBack();
writeln(c.back); // "b"


and it worked fine:
My Addition abc
b
abc
a
c
b


What gives?  Thanks.




More information about the Digitalmars-d-learn mailing list