Search code examples
c#visual-studiofindsymbols

How to find a symbol like a *?


class Program
{
    static void Main(string[] args)
    {
        string[] lines = System.IO.File.ReadAllLines("C:\\Users\\mrazekd\\Downloads\\PrubehPripravyPat.txt");
        string regMatch = "***";
        foreach (string line in lines)
        {
            if (Regex.IsMatch (line, regMatch))
            {
                Console.WriteLine("found\n");
            }
            else
            {
                Console.WriteLine("not found\n");
            }
        }
    }
}

This code find only number or letter but dont find symbol like star. What Im doing wrong? In my file is a lot of star but it still doesn't find one and it lists the error that the search value wasn't specified.


Solution

  • You have to escape it with @ and \ see here: https://www.codeproject.com/Articles/371232/Escaping-in-Csharp-characters-strings-string-forma

    using System;
    using System.Text.RegularExpressions; 
    
    class Program
    {
        static void Main(string[] args)
        {
            string[] lines = System.IO.File.ReadAllLines("C:\\Users\\mrazekd\\Downloads\\PrubehPripravyPat.txt");
            string regMatch = @"\*";
            foreach (string line in lines)
            {
                if (Regex.IsMatch (line, regMatch))
                {
                    Console.WriteLine("found\n");
                }
                else
                {
                    Console.WriteLine("not found\n");
                }
            }
        }
    }