0

このような文字列で置換を行う方法はありますか?(これは簡略化された例です)

string input = "INSERT INTO blah VALUES \" blah blah \r\n \" \r\n (here can be anything like several new lines and \t tabs) INSERT INTO blah VALUES \" blah blah \r\n \" \r\n";

input.Replace("\r\n", "\n"); // this is just example, it doesn't work the way I need it

// and the output would look like this:
string output= "INSERT INTO blah VALUES \" blah blah \r\n \" \n INSERT INTO blah VALUES \" blah blah \r\n \" \n";

では、SQLコマンドの外側でのみ新しい行を置き換えますか?これは正規表現を使用して安全に実現できますか?

編集:置換はおそらく\ r\nから\nで実現する必要があります。SQLコマンド間にはさらに多くの置換が存在する可能性があります。コマンドは正確に分離されていません。

編集:基本的な問題は-外側の文字列だけを置き換えるにはどうすればよいですか?

string = "outer string \"inner string\" outer string \"inner string\" outer string"
4

2 に答える 2

1

こんなの欲しいのか……。

/// <summary>
///  Regular expression built for C# on: Wed, Aug 29, 2012, 09:56:25 AM
///  Using Expresso Version: 3.0.4334, http://www.ultrapico.com
///  
///  A description of the regular expression:
///  
///  [1]: A numbered capture group. [(?<counter>").*?(?<-counter>").*?(?(counter)(?!))]
///      (?<counter>").*?(?<-counter>").*?(?(counter)(?!))
///          [counter]: A named capture group. ["]
///              "
///          Any character, any number of repetitions, as few as possible
///          Balancing group. Remove the most recent [counter] capture from the stack. ["]
///              "
///          Any character, any number of repetitions, as few as possible
///          Conditional Expression with "Yes" clause only
///              Did the capture named [counter] match?
///              If yes, search for [(?!)]
///                  Match if suffix is absent. []
///                      NULL
///  \r\n
///      Carriage return
///      New line
///  
///
/// </summary>
Regex regex = new Regex(
      "((?<counter>\").*?(?<-counter>\").*?(?(counter)(?!)))\\r\\n",
    RegexOptions.CultureInvariant
    | RegexOptions.Compiled
    | RegexOptions.Singleline
    );

string input = "INSERT INTO blah VALUES \" blah blah \r\n \" \r\n (here can be anything like several new lines and \t tabs) INSERT INTO blah VALUES \" blah blah \r\n \" \r\n";

string output output=regex.Replace(input,"$1\n");

の効果は、(?<counter>").*?(?<-counter>").*?(?(counter)(?!))バランス"文字のみに一致するため、引用符の外側の \r\n のみを見つけることです

于 2012-08-29T09:02:43.280 に答える
0

あなたはと交換したいように聞こえます\r\n \" \r\n\r\n \" \n

input = input.Replace("\r\n \" \r\n", "\r\n \" \n");
于 2012-08-29T08:16:28.787 に答える