-1
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.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
dejf111
  • 39
  • 8
  • 1
    Why use Regex here? Use normal [string methods](http://zetcode.com/csharp/searchstring/) instead. If you insist on using Regex, please [escape](https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.escape?view=netcore-3.1) the `*` (`\*`). – Uwe Keim Sep 16 '20 at 07:33
  • Can I use string method? – dejf111 Sep 16 '20 at 07:34
  • 1
    There is [`String.Contains`](https://learn.microsoft.com/en-us/dotnet/api/system.string.contains) – Hans Kesting Sep 16 '20 at 10:36

1 Answers1

0

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");
            }
        }
    }
}