本质上,我有一个用于插值的字符串:Log_{0}.txt并且{0}在不同的过程中被替换为整数。所以结果可能类似于Log_123.txtor Log_53623432.txt。
我正在尝试使用string.Format()并替换{0}为检查数字的正则表达式。最终,我也希望能够提取这些数字。
我已经尝试过类似这样的一些不同的变化,但我没有任何运气:
var stringFormat = new Regex(string.Format("Log_{0}.txt", @"^\d$"));
此外,这是检查格式的代码:
var fileNameFormat = new Regex(string.Format("Log_{0}.txt", @"^\d+$"));
var existingFiles = Directory.GetFiles("c:\\projects\\something");
foreach(var file in existingFiles)
{
var fileName = Path.GetFileName(file);
if(fileNameFormat.Match(fileName).Success)
{
// do something here
}
}
守候你守候我
MMTTMM
相关分类