0

I created my own regex, and everything work fine except the backslash thing. I tried my versions, but none of them helped.

var regexItem = new Regex("[^A-Za-z0-9_.,&/*:;=+{}()'\"\\ -]+");
string temp2 = "";
while ((@line = file2.ReadLine()) != null)
{
    if (regexItem.IsMatch(line) && (line.Contains(".jpg") || line.Contains(".png") || line.Contains(".jpeg") || line.Contains(".svg")))
    {
        @temp2 = Regex.Replace(line, "[^A-Za-z0-9_.,&/\\*:;=+{}()'\" -]+", "");
        postki.WriteLine(@temp2);
        Console.WriteLine(@"{0} ==> {1}", @line, @temp2);
    }
    else
    {
        postki.WriteLine(@line);
    }
}

Spevacus
  • 584
  • 2
  • 13
  • 23
FloodXツ
  • 9
  • 1
  • 3
  • 3
    Backslash is a special character in _both_ regex and C#. If you want to use a literal backslash in your pattern, then you need to escape it both in regex and in C#. – Kenneth K. Oct 01 '19 at 16:50
  • Actually, I don't understand it. :'D – FloodXツ Oct 01 '19 at 16:58
  • Sometimes it's easier to pull out characters that need to be escaped: `char doubleQuote = '"'; string s = "he said," + doubleQuote + "hello!" + doubleQuote;` – BurnsBA Oct 01 '19 at 17:10

1 Answers1

1

In c# string literals, special characters (like ") need to be escaped. C# uses the \ to escape those characters.

For example - as you already did - a string containing a " would be declared like that:

string quote = "\"";

So of course the backslash itself needs escapting, too. So a string containing a back-slash would look like this:

string backslash = "\\";

So with two backslashs in the literal, you have one real backslash in the string.

But a backslash also is a special character in regular expressions (also used to escape other symbols, for example if you mean a literal . instead of the any-character-dot, you use \.). So to use a literal backslash (meaning to match a single backslash in the search string) you need to use 4 back-slashes in your c# string literal:

string regexBackSlash = "\\\\";

In your regex you probably meant:

var regexItem = new Regex("[^A-Za-z0-9_.,&/*:;=+{}()'\"\\\\ -]+");
                                     // difference here ^

Or you may use verbatim string literals (with a leading @):

var regexItem = new Regex(@"[^A-Za-z0-9_.,&/*:;=+{}()'""\\ -]+");

(but then again you need to escape the " with a second one).

René Vogt
  • 43,056
  • 14
  • 77
  • 99