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

Dmitry Olshansky dmitry.olsh at gmail.com
Thu May 30 09:31:04 PDT 2013


30-May-2013 10:49, Timothee Cour пишет:
> 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.

One thing missing that '.' that should become '\.'.

>
>  > 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?

Yes, please. It's was a blind spot for long time. Strictly speaking I 
think that a generic escaping routine would work:

auto escape(S1, S2, C)(S1 src, S2 escapables, C escape='\\')
	if(isSomeString!S1 && isSomeString!S2 && isSomeChar!C)
{
	....
}

Do we have something like this in std.string?
Then all we need is a convenience wrapper in std.regex?

BTW unescape is as important.

>
> On Wed, May 29, 2013 at 8:32 PM, Diggory <diggsey at googlemail.com
> <mailto: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.
>
>


-- 
Dmitry Olshansky


More information about the Digitalmars-d-learn mailing list