Regex question

simendsjo simendsjo at gmail.com
Wed Mar 28 03:04:57 PDT 2012


On Wed, 28 Mar 2012 11:40:21 +0200, James Blewitt <jim at 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));
}


More information about the Digitalmars-d-learn mailing list