[Issue 2104] New: Escape function for regular expressions
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Tue May 13 02:17:17 PDT 2008
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 at digitalmars.com
ReportedBy: gaboonviper at 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
--
More information about the Digitalmars-d-bugs
mailing list