C#替换正则表达式 [英] C# Replace with regex
问题描述
我是新的VB,C#,和我与正则表达式挣扎。我想我已经得到了下面的代码格式来取代正则表达式匹配,在我的文件空格
编辑:每评论评论这个代码块已被更改。 p>
VAR fileContents = System.IO.File.ReadAllText(@C:\path\to\file.csv) ;
fileContents = fileContents.Replace(fileContents,@正则表达式,);
正则表达式=新的正则表达式(模式);
regex.Replace(filecontents,);
System.IO.File.WriteAllText(@C:\path\to\file.csv,fileContents);
我的文件的格式是这样的:
1111111,22222222222,文本可能,有一个逗号,或者二,2014年9月1日,,,,,,
到目前为止,我正则表达式找到和
包含一个逗号(有没有在第一个或最后一个单元格逗号,所以我不担心不包括两项。我在的快报
(小于?=)([^] +,[^] +)。(?=,)
我只是不知道如何处理逗号作为隔离需要更换的东西。什么是做到这一点的最好方法是什么?
解决:
合并[^] +背后/提前看:
(小于=[ ^] +)(,)(?= [^] +)
最后编辑:
这里是我最后的完整的解决方案:
//读取文件内容
VAR fileContents = System.IO .File.ReadAllText(@C:\path\to\file.csv);
//找到双引号
变种的regex =新的正则表达式(之间的所有逗号(小于?=,\)([^ \] + [^ \ ] +(= \?));
//与替换所有的逗号,
fileContents = regex.Replace(fileContents,M => m.ToString( ).Replace(,));
//结果写入回文件
System.IO.File.WriteAllText(@C:\path\to \file.csv,fileContents);
我可能会使用Regex.Replace的,需要一个代表超载返还替换文本。当你有一个简单的正则表达式来识别模式
这是有用的,但你需要做的东西少直接的(复杂的逻辑)更换。
我觉得让你的正则表达式简单的将支付的好处,当你试图以后维护。
注意:这是类似于@Florian答案,但这种替换限制自己只更换了匹配的文本
串EXP =(小于?=,\)([^ \] +,[^ \] +)(= \?,);
变种的regex =新的正则表达式(EXP);
串replacedtext = regex.Replace(filecontents,M =方式> m.ToString()更换(,))
I'm new to VB, C#, and am struggling with regex. I think I've got the following code format to replace the regex match with blank space in my file.
EDIT: Per comments this code block has been changed.
var fileContents = System.IO.File.ReadAllText(@"C:\path\to\file.csv");
fileContents = fileContents.Replace(fileContents, @"regex", "");
regex = new Regex(pattern);
regex.Replace(filecontents, "");
System.IO.File.WriteAllText(@"C:\path\to\file.csv", fileContents);
My files are formatted like this:
"1111111","22222222222","Text that may, have a comma, or two","2014-09-01",,,,,,
So far, I have regex finding any string between ,"
and ",
that contains a comma (there are never commas in the first or last cell, so I'm not worried about excluding those two. I'm testing regex in Expresso
(?<=,")([^"]+,[^"]+)(?=",)
I'm just not sure how to isolate that comma as what needs to be replaced. What would be the best way to do this?
SOLVED: Combined [^"]+ with look behind/ahead:
(?<=,"[^"]+)(,)(?=[^"]+",)
FINAL EDIT: Here's my final complete solution:
//read file contents
var fileContents = System.IO.File.ReadAllText(@"C:\path\to\file.csv");
//find all commas between double quotes
var regex = new Regex("(?<=,\")([^\"]+,[^\"]+(?=\",)");
//replace all commas with ""
fileContents = regex.Replace(fileContents, m => m.ToString().Replace(",", ""));
//write result back to file
System.IO.File.WriteAllText(@"C:\path\to\file.csv", fileContents);
I would probably use the overload of Regex.Replace that takes a delegate to return the replaced text. This is useful when you have a simple regex to identify the pattern but you need to do something less straightforward (complex logic) for the replace.
I find keeping your regexes simple will pay benefits when you're trying to maintain them later.
Note: this is similar to the answer by @Florian, but this replace restricts itself to replacement only in the matched text.
string exp = "(?<=,\")([^\"]+,[^\"]+)(?=\",)";
var regex = new Regex(exp);
string replacedtext = regex.Replace(filecontents, m => m.ToString().Replace(",",""))
这篇关于C#替换正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!