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
「提高」的定義是什麼?更快?更低的內存佔用?更正確? –