これは、テキストの引用部分で区切り文字をエスケープできる正規表現ソリューションです。*nix バックスラッシュ スタイルのエスケープを使用する場合は、正規表現の適切な部分を , に置き換えるだけ('')
です(\\')
。
static readonly Regex rxQuotedStringLiteralPattern = new Regex(@"
# A quoted string consists of
' # * a lead-in delimiter, followed by
(?<content> # * a named capturing group representing the quoted content
( # which consists of either
[^'] # * an ordinary, non-delimiter character
| # OR
('') # * an escape sequence representing an embedded delimiter
)* # repeated zero or more times.
) # The quoted content is followed by
' # * the lead-out delimiter
"
, RegexOptions.ExplicitCapture|RegexOptions.IgnorePatternWhitespace
) ;
public static IEnumerable<string> ParseQuotedLiteralsFromStringUsingRegularExpressions( string s )
{
for ( Match m = rxQuotedStringLiteralPattern.Match( s ?? "" ) ; m.Success ; m = m.NextMatch() )
{
string raw = m.Groups[ "content" ].Value ;
string cooked = raw.Replace( "''" , "'" ) ;
yield return cooked ;
}
}