digitalmars.D.learn - Regex question
- James Blewitt (13/13) Mar 28 2012 I'm having a problem with regexes.
- simendsjo (22/34) Mar 28 2012 Sounds like a bug. The following works:
- Dmitry Olshansky (29/35) Mar 28 2012 Please include compilation errors in future, it helps folks to figure
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. I'm using DMD v2.058 Any ideas what is going on? ------ import std.regex; Regex!(char) testRegex = regex("\\b(A(Z)?|B(Z)?|C(Z)?)\\b", "i"); void main() { // Regex!(char) testRegex = regex("\\b(A(Z)?|B(Z)?|C(Z)?)\\b", "i"); }
Mar 28 2012
On Wed, 28 Mar 2012 11:40:21 +0200, James Blewitt <jim jblewitt.com> 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. I'm using DMD v2.058 Any ideas what is going on? ------ import std.regex; Regex!(char) testRegex = regex("\\b(A(Z)?|B(Z)?|C(Z)?)\\b", "i"); void main() { // Regex!(char) testRegex = regex("\\b(A(Z)?|B(Z)?|C(Z)?)\\b", "i"); }Sounds like a bug. The following works: import std.regex; Regex!(char) testRegex = ctRegex!(r"\b[ABC]Z?\b", "i"); void main() { // matches assert(match(" A ", testRegex)); assert(match(" B ", testRegex)); assert(match(" C ", testRegex)); assert(match(" AZ ", testRegex)); assert(match(" BZ ", testRegex)); assert(match(" CZ ", testRegex)); // case insensitive assert(match(" a ", testRegex)); assert(match(" az ", testRegex)); // needs match at word boundary assert(!match("A", testRegex)); // doesn't match other characters assert(!match(" D ", testRegex)); assert(!match(" DZ ", testRegex)); assert(!match(" DZ ", testRegex)); }
Mar 28 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
Mar 28 2012