Search and Replace to include multiple lines (using DelphiScript)
I have these two lines in a text file:
string1
string2
I want to find these two lines and replace with a single line:
string3
The scripts that I have is as follows:
//Create a regular expression
regEx := HISUtils.RegExp;
//Set regular expression pattern
regEx.Expression := 'string1(\r\n)string2';
//Read from file
fileText := aqFile.ReadWholeTextFile(srcFile, aqFile.ctANSI);
//Perform replace operation
fileMod := regEx.Replace(fileText, 'string3');
//Write back to file
aqFile.WriteToTextFile(srcFile, fileMod, aqFile.ctANSI, true);
Search and replace does not happen with the above codes. If the string that I want to replace is within a single line, it all works fine but does not work when it involves multiple lines. The issue is definitely to do with multiple lines of text but I am not sure how can I detect this?
Any help would be much appreciated.
The file contains only newlines (and no carriage returns). Change your search expression to either of the following
runlogRegEx.Expression := 'string1'+#10+'string2'; ... runlogRegEx.Expression := 'string1\nstring2';
The regular expression syntax used by HISUtils.RegExpr is more completely described here: