如何找到像*这样的符号?
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");
}
}
}
}
此代码只能找到数字或字母,但找不到像星号这样的符号。我做错了什么?在我的文件中有很多星号,但仍然找不到星号,它列出了未指定搜索值的错误。
你必须使用 @ 和 \ 来转义它,请看这里: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");
}
}
}
}
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");
}
}
}
}
此代码只能找到数字或字母,但找不到像星号这样的符号。我做错了什么?在我的文件中有很多星号,但仍然找不到星号,它列出了未指定搜索值的错误。
你必须使用 @ 和 \ 来转义它,请看这里: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");
}
}
}
}