0
我试图编辑一个字符串在C#中使用正则表达式。在阅读下面的预期结果后;我的正则表达式是否正确?如果不正确,我该如何改进它们?这是正则表达式正确:你将如何改善它
方法:
- 对于每一个“{”字符:确保它不会坐在一个空行(领先&尾随空白&格式字符之和构成一个空 线)
- 对于每个“}”字符:确保它位于自己的行上,没有其他文字
例如:
string super
\v\t { abc
colour "red" }
要这样:从这个转换
string super {
abc
colour "red"
}
我的代码:
public string[] formatFileContents(string fileContents) {
Regex openBrkRgx = new Regex(@"([\c\s]+){");
// does \c capture whitespace chars?
fileContents = openBrkRgx.Replace(fileContents, "{\r\n");
Regex closedBrkRgx = new Regex(@"[\d\w]}[\d\w]");
// is the above regex comprehensive in your opinion -
// ie, can you think of areas where this might fail?
fileContents = rgx.Replace(fileContents, "\r\n}\r\n");
}
你试过你的代码了吗? – zneak
“提高”的定义是什么?更快?更低的内存占用?更正确? –