Jan Matousek
Jan Matousek

Reputation: 1006

C# string replace in outer string only

Is there a way to do a replace in a string like this? (this is simplified example)

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";

So it would replace new lines only outside the SQL commands? Can this be safely achieved using regular expressions?

EDIT: the replace must be probably realised with \r\n to \n there could be more of them between the SQL commands. The commands are not precisely separated.

EDIT: so the basic problem is - how do I replace in the outer string only?

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

Upvotes: 0

Views: 236

Answers (2)

Bob Vale
Bob Vale

Reputation: 18474

Do you want something like this....

/// <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");

The effect of (?<counter>").*?(?<-counter>").*?(?(counter)(?!)) is to match only balancing " characters so to only find the \r\n outside the quotes

Upvotes: 1

Chibueze Opata
Chibueze Opata

Reputation: 10054

You sound like you want to replace \r\n \" \r\n with \r\n \" \n

input = input.Replace("\r\n \" \r\n", "\r\n \" \n");

Upvotes: 0

Related Questions