Issues with std.regex

H. S. Teoh hsteoh at quickfur.ath.cx
Sat Feb 16 12:33:46 PST 2013


On Sat, Feb 16, 2013 at 09:22:07PM +0100, MrAppleseed wrote:
> Hey all,
> 
> I'm currently trying to port my small toy language I invented awhile
> back in Java to D. However, a main part of my lexical analyzer was
> regular expression matching, which I've been having issues with in
> D. The regex expression in question is as follows:
> 
> [ 0-9a-zA-Z.*=+-;()\"\'\[\]<>,{}^#/\\]
> 
> This works well enough in Java to produce a series of tokens that I
> could then pass to my parser. But when I tried to port this into D,
> I almost always get an error when using brackets, braces, or
> parenthesis. I've tried several different combinations, have looked
> through the std.regex library reference, have Googled this issue,
> have tested my regular expression in several online-regex testers
> (primarily http://regexpal.com/, and http://regexhelper.com/), and
> have even looked it up in the book, "The D Programming Language"
> (good book, by the way), yet I still can't get it working right.
> Here's the code I've been using:
> 
> ...
> auto tempCont = cast(char[])read(location, fileSize);
> string contents = cast(string)tempCont;
> auto reg = regex("[ 0-9a-zA-Z.*=+-;()\"\'\[\]<>,{}^#/\\]");

The problem is that you're using D's double-quoted string literal, which
adds another level of interpretation to the \'s. What you should do is
to use the backtick string literal, which does *not* interpret
backslashes:

auto reg = regex(`[ 0-9a-zA-Z.*=+-;()\"\'\[\]<>,{}^#/\\]`);

If you have trouble typing `, you can also use r"...", which means the
same thing.

Hope this helps.


--T


More information about the Digitalmars-d-learn mailing list