【发布时间】:2021-08-17 01:49:59
【问题描述】:
我有一个正则表达式新手问题。
我正在尝试在字符串中进行不区分大小写的搜索。我需要搜索 cgif 后跟任何文本并以 .txt 结尾,但下面的代码不起作用。我错过了什么?
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
var rx = new Regex("(cgif)+(.txt)+^", RegexOptions.Compiled | RegexOptions.IgnoreCase);
bool x1=rx.IsMatch("abc\\cgif123.txt");
Console.WriteLine($"{x1}");<=should return true
bool x2=rx.IsMatch("abc\\cgif.txt");
Console.WriteLine($"{x2}");<=should return true
bool x3=rx.IsMatch("abc\\cgif.txtabc");
Console.WriteLine($"{x3}");<=should return false
}
}
【问题讨论】:
-
您学习了哪些正则表达式教程?将
@"cgif.*\.txt$"与| RegexOptions.Singleline一起使用