-1

SQL グループ化条件で、フィールドをキャストされたバージョンのフィールドに置き換えたい:

例えば:

input: sum(count(andrei) + count(2) + sum(count3)

ouptput: sum(count(cast(andrei as int)) + count(cast(2 as int)) + sum(cast(count3 as int))

私の考えは、次のパターンで「(」または「)」を含まないリテラルを見つけることです。

  Match m = Regex.Match(input, "\\([^\\(\\)]+\\)");

キャストされたバージョンに置き換えます。

置換の実行方法がわかりません。

4

1 に答える 1

1

次のパターンと置換文字列を使用できます。

パターン:(?<=\()([^()]+)(?=\))

  • (?<=\(): 左括弧に一致する (ただし消費しない) 後読み
  • ([^()]+): 開始括弧と終了括弧以外に一致する負の文字クラスを持つ番号付きキャプチャ グループ。文字クラス内に出現する場合、エスケープする必要はありません。
  • (?=\)): 閉じ括弧を検出する先読み

置換文字列:最初の番号付きキャプチャ グループを参照しcast($1 as int)ます。$1

string input = "sum(count(andrei)) + count(2) + sum(count3)";
string pattern = @"(?<=\()([^()]+)(?=\))";
string replacement = "cast($1 as int)";
string result = Regex.Replace(input, pattern, replacement);
Console.WriteLine(result);
于 2013-08-17T05:29:39.687 に答える