0

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"
2
  • 4
    If you are using it for SQL statements I strongly recommend SqlParameters: dotnetperls.com/sqlparameter Commented Aug 29, 2012 at 8:09
  • 4
    Replace returns new string so code should look like input = input.Replace("\r\n", "\n"); Commented Aug 29, 2012 at 8:09

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

the amount of new lines between the sql commands are iregular and the commands can be different

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.