Yannam
Yannam

Reputation: 21

Remove text in between delimiters in a string - regex? when the string has '/'

This is the string I have. Script:

Select * from tablename()

/*

go

*/

My code looks like this

private string RemoveBetween(string editScript, char begin, char end)
{
   Regex regex = new Regex(string.Format("///.*?///", begin, end));
   return  regex.Replace(editScript, string.Empty);
}

editscript = RemoveBetween(editscript, '/', '/');

AM unable to remove the text between /*----*/.

Upvotes: 1

Views: 516

Answers (1)

decPL
decPL

Reputation: 5402

private string RemoveBetween(String editScript, String begin, String end)
{
   return Regex.Replace(editScript,
                        String.Format("{0}.*?{1}", begin, end),
                        String.Empty,
                        RegexOptions.Singleline)
}

usage:

editscript = RemoveBetween(editscript, "/", "/");

I've changed your signature to use String, because you probably might want to call it like so (to remove SQL comment blocks):

editscript = RemoveBetween(editscript, "/\*", "\*/");

Upvotes: 1

Related Questions