www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - regex with literal (ie automatically replace '(' with '\(', etc) )

See below:

import std.stdio;
import std.regex;

void main(){
	"h(i".replace!(a=>a.hit~a.hit)(regex(`h\(`,"g")).writeln; //this 
works, but I need to specify the escape manually
//	"h(i".replace!(a=>a.hit~a.hit)(regex(`h(`,"gl")).writeln;  
//I'd like this to work with a flag, say 'l' (lowercase L) as in 

note, std.array.replace doesn't work because I want to be able to 
use std.regex' replace with delegate functionality as above.
This is especially useful when the regex's first argument is 
given as an input argument (ie is unknown), and we want to 
properly escape it.

Alternatively, (and perhaps more generally), could we have a 
string toRegexLiteral(string){
//replace all regex special characters (like '(' ) with their 
escaped equivalent
May 29 2013