www.digitalmars.com         C & C++   DMDScript  

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

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2104

           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");
}

Usage:

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) ~ "|" ~
regEscape(needle2)).search(haystack);

Cheers,
Boyd


-- 
May 13 2008
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2104


Dmitry Olshansky <dmitry.olsh gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dmitry.olsh gmail.com
            Version|2.013                       |D2
         AssignedTo|nobody puremagic.com        |dmitry.olsh gmail.com



03:31:32 PDT ---
Makes sense for new std.regex as well.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 12 2013