要根据您的规范用单引号替换双引号,请使用这个简单的正则表达式。此正则表达式将允许行首和/或行尾有空格。
string pattern = @"(?<!^\s*|,)""(?!,""|\s*$)";
string resultString = Regex.Replace(subjectString, pattern, "'", RegexOptions.Multiline);
这是模式的解释:
// (?<!^\s*|,)"(?!,"|\s*$)
//
// Options: ^ and $ match at line breaks
//
// Assert that it is impossible to match the regex below with the match ending at this position (negative lookbehind) «(?<!^\s*|,)»
// Match either the regular expression below (attempting the next alternative only if this one fails) «^\s*»
// Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
// Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s*»
// Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
// Or match regular expression number 2 below (the entire group fails if this one fails to match) «,»
// Match the character “,” literally «,»
// Match the character “"” literally «"»
// Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!,"|\s*$)»
// Match either the regular expression below (attempting the next alternative only if this one fails) «,"»
// Match the characters “,"” literally «,"»
// Or match regular expression number 2 below (the entire group fails if this one fails to match) «\s*$»
// Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s*»
// Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
// Assert position at the end of a line (at the end of the string or before a line break character) «$»