我有文件的文件名erwin_01problem.doc,我想要的是如果文件名不包含 01problem 之间的空格(erwin_0 1p roblem.doc) 。我找到问题的索引并用“”替换它。输出将是 erwin_01 problem.doc 这是我尝试的代码,但我仍然无法在01和问题之间放置一个空格。
if (!string.IsNullOrEmpty(job.ProblemPath))
{
job.HasProblemFile = true;
var problemDocFname = Path.GetFileName(job.ProblemPath);
if (!Regex.IsMatch(problemDocFname, @"\sproblem\.doc$"))
{
ProgM.JobStatus = "Checking space between filename and problem...";
Thread.Sleep(1000);
problemDocFname = problemDocFname.Insert(problemDocFname.IndexOf("problem.doc", StringComparison.Ordinal), " ");
//problemDocFname = problemDocFname.Replace("problem", " problem");
}
problemDocFname = Path.Combine(job.FilePath, problemDocFname);
var docProblemCount = 0;
ProgM.JobStatus = "Correcting the Format of Problem Doc...";
Thread.Sleep(1000);
MicrosoftWord.CorrectProblemDocFormatting(problemDocFname, ref docProblemCount);
}
jobs.Add(job);
答案 0 :(得分:0)
我相信你真的不需要正则表达式。你可以这样做:
string key = "problem.doc";
if (problemDocFname.EndsWith(key) && problemDocFname.Length > key.Length)
{
problemDocFname.Replace(key, " problem.doc");
}