Regex question

Dmitry Olshansky dmitry.olsh at gmail.com
Wed Mar 28 03:22:39 PDT 2012


On 28.03.2012 13:40, James Blewitt wrote:
> I'm having a problem with regexes.
> The following code gives a compilation error. If I comment out the regex
> outside of main and comment in the regex inside of main then it does
> compile.

Please include compilation errors in future, it helps folks to figure 
the cause even without compiling your code.

In this case I get:

C:\dmd2\windows\bin\..\..\src\phobos\std\regex.d(1220): Error: 
assert(cast(int)
his.ir[orStart].code() == 129) failed
C:\dmd2\windows\bin\..\..\src\phobos\std\regex.d(946):        called 
from here:
this.parseRegex()
C:\dmd2\windows\bin\..\..\src\phobos\std\regex.d(6522):        called 
from here
  parser.this(pattern,flags)
newww.d(4):        called from here: regex("\\b(A(Z)?|B(Z)?|C(Z)?)\\b","i")
Failed: "dmd" "-v" "-o-" "newww.d" "-I."

 >
 > I'm using DMD v2.058
 >
 > Any ideas what is going on?
 >

It does look like a bug C-T parser, this one I'm aware of, but I have no 
cure for it yet.
Apparently global variables requires const initializers thus invoking 
CTFE to process regex(...).

Try something like this for a workaround:

Regex!(char) testRegex;

static this(){
	testRegex = regex("\\b(A(Z)?|B(Z)?|C(Z)?)\\b", "i");
//init all global
}

P.S. Note that you have raw literals in D, it is advised to use them for 
regex: `\b(A(Z)?|B(Z)?|C(Z)?)\b` or r"\b(A(Z)?|B(Z)?|C(Z)?)\b"

-- 
Dmitry Olshansky


More information about the Digitalmars-d-learn mailing list