regex with literal (ie automatically replace '(' with '\(', etc) )

Timothee Cour thelastmammoth at gmail.com
Wed May 29 23:49:51 PDT 2013


ok, here it is:

https://github.com/timotheecour/dtools/blob/master/dtools/util/util.d#L78
simplified implementation and added missing escape symbols. Any symbol
missing?
I was basing myself based on http://dlang.org/phobos/std_regex.html, table
entry '\c where c is one of', but that was incomplete. I'm also noting that
table entry 'any character except' is also incomplete.

> Technically any working "escapeRegex" would also function as a valid
"escapeRegexReplace", although it might be slightly faster to have a
specialised one.

not sure, because they escape differently (\$ vs $$).

shall i do a pull request for std.regex?


On Wed, May 29, 2013 at 8:32 PM, Diggory <diggsey at googlemail.com> wrote:

> On Wednesday, 29 May 2013 at 23:33:30 UTC, timotheecour wrote:
>
>> something like this, which we should have in std.regex:
>>
>> string escapeRegex(string a){
>>         import std.string;
>>         enum transTable = ['[' : `\[`, '|' : `\|`, '*': `\*`, '+': `\+`,
>> '?': `\?`, '(': `\(`, ')': `\)`];
>>         return translate(a, transTable);
>> }
>> string escapeRegexReplace(string a){
>>         import std.string;
>> //      enum transTable = ['$' : `$$`, '\\' : `\\`];
>>         enum transTable = ['$' : `$$`];
>>         return translate(a, transTable);
>> }
>>
>> unittest{
>>         string a=`asdf(def[ghi]+*|)`;
>>         assert(match(a,regex(**escapeRegex(a))).hit==a);
>>         string b=`$aa\/$ $$#@$\0$1#$@%#@%=+_`;
>>         auto s=replace(a,regex(escapeRegex(**a)),escapeRegexReplace(b));
>>         assert(s==b);
>> }
>>
>
> That would be good (although you missed a few :P)
>
> Technically any working "escapeRegex" would also function as a valid
> "escapeRegexReplace", although it might be slightly faster to have a
> specialised one.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20130529/44d14cae/attachment.html>


More information about the Digitalmars-d-learn mailing list