我在尝试使用C#替换单词之前的文本时遇到问题,我想删除在特定单词之前找到的所有文本。我尝试了这个但是没用
string filecomplete = File.ReadAllText(Path.GetFullPath(ofd.FileName));
string nonimportant = ".*?(?=WORD)";
string file = System.Text.RegularExpressions.Regex.Replace(filecomplete,
nonimportant, string.Empty);
我想要的是保留WORD之后的所有文本并删除之前的所有内容。
谢谢,
以下选项使用MatchEvaluator
Regex.Replace的语法。请注意,您只需要放置WORD并通过捕获捕获组中的后续内容()
。
string nonimportant = ".*?(WORD.*)";
string st = "aslkfdalksdWORDewot";
string file = Regex.Replace(st, nonimportant, (Match m) => String.Format("{0}",m.Groups[1]));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句