Bug 2104 – std.regex: escape function for regular expressions

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
x86
OS
All
Creation time
2008-05-13T04:17:00Z
Last change time
2016-04-14T15:53:05Z
Keywords
pull
Assigned to
dmitry.olsh
Creator
gaboonviper

Comments

Comment #0 by gaboonviper — 2008-05-13T04:17:09Z
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
Comment #1 by dmitry.olsh — 2013-08-12T03:31:32Z
Makes sense for new std.regex as well.
Comment #2 by dmitry.olsh — 2016-04-06T07:56:06Z
Comment #3 by github-bugzilla — 2016-04-14T15:53:04Z
Commits pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/cf6152c13fb1215f6e97c79f8189ce7a015a047b Fix issue 2104 - escape function for regular expressions https://github.com/D-Programming-Language/phobos/commit/4e393eb70a99599811f74a991a5685388c689243 Merge pull request #4156 from DmitryOlshansky/issue-2104 Fix issue 2104 - escape function for regular expressions