regex, IsMatch有什么不同

本文关键字:什么 IsMatch regex | 更新日期: 2023-09-27 18:02:15

我试图找到遵循"字符'h'后跟一个或多个数字"模式的文件,如h0, h1, h22

但我也得到"h22 cco"和hhh0

如何修复

files = new List<String>((from file in files where Regex.IsMatch(Path.GetFileNameWithoutExtension(file), "h''d+", RegexOptions.IgnoreCase | RegexOptions.Singleline) select Path.GetFileNameWithoutExtension(file)));

regex, IsMatch有什么不同

使用行首和行尾特殊字符^$,即:^h+'d+$

const string Pattern = @"^h+'d+$";

它将匹配字符串,字符串的开头包含一个或多个h,然后重复一个或多个数字。示例:h0, h34, hh5, hhhh789 .

如果一开始只需要一个h,使用这个正则表达式:^h'd+$。它将匹配:h0, h34, h789323

^表示:匹配字符串中的起始位置。在基于行的工具中,它匹配任何行的起始位置。

我猜你需要这种模式:

Regex regexObj = new Regex(@"'bh'd+'b", RegexOptions.Singleline | RegexOptions.IgnoreCase);

' b是指单词边界