www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2104] New: Escape function for regular expressions


           Summary: Escape function for regular expressions
           Product: D
           Version: 2.013
          Platform: PC
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: gaboonviper gmx.net

I've been using regular expressions extensively and found that there was no
escape function in the std.regexp library. It's useful when using user input in
regular expressions.

Here's a simple one I built. It's probably not the most efficient one out
there, but feel free to use it.

string regEscape(string aExpression)
    return sub(aExpression, r"[\\\.\*\+\?\^\$\[\]\{\}\(\)\|]", "\\$&", "g");


string needle1 = r"[needle]";
string needle2 = r"(needle)";

string haystack = r"[needle] (needle)";

// find needles 1 and/or 2 in the haystack
auto result = RegExp(regEscape(needle1) ~ "|" ~


May 13 2008