Replacing Text

We pointed out in the introduction to this tutorial that errors may be introduced as a result of copying code. In our experience this is a common source of errors in students' programs. You will introduce errors if you change manually the names of variables in the copied code to match your existing declarations but fail to complete the task. If the unchanged variable names happen to have been declared for another purpose in your program, this will not generate a syntax error and the consequence is likely to be serious. If you use the editor's Search & Replace facility, you should increase your chances of making all the necessary replacements but you may make additional unwanted changes if you are not careful. We advise you to save a copy of your code before carrying out the replacements.

Using Regular Expressions

Regular expressions greatly increase your options when replacing code. We provide an example of a regular expression that adds formatting code to the following short program.

program RealDemo;
  {$APPTYPE CONSOLE}
uses
  SysUtils;
var
  Real1, Real2, Sum, Difference, Product : real;
begin
  write('Please enter first real number. ');
  readln(Real1);
  write('Please enter second real number. ');
  readln(Real2);
  Sum := Real1 + Real2;
  Difference := Abs(Real1 - Real2);
  Product := Real1 * Real2;
  write('Sum: ');
  write(Sum);
  write('   Difference: ');
  write(Difference);
  write('   Product: ');
  write(Product);
  readln;
end.

The unsatisfactory output from Delphi 7 is:

Unsatisfactory formatting

Unsatisfactory formatting

We want to add the formatting code : 5 : 2 to the write statements that are outputting variables but not strings. We used this replace dialog in Delphi 7 to achieve the changes:

Replace Dialog formatting

Replace Dialog

We put text to be replaced is in braces. (Several search patterns could be handled at the same time, each within its own set of braces). The backslash before the round bracket is to indicate that the bracket is a character within the search pattern and not a regular expression control character. After searching for write( we exclude strings by putting the NOT character ^ before the string indicator '. The next expression in square brackets represents, as you would expect, any lower or upper case letter. The * means that any number of letters can be part of the search pattern.

In the Replace with edit box, the \0 represents an unchanged copy of the first (and in our case the only) search pattern.

A screenshot of the resulting code in Delphi 7 shows that we achieved our objective.

Replace Dialog

Resulting code after find and replace

This code gave us the output that we required:

Satisfactory formatting

Satisfactory formatting

In Lazarus, we put (write\([^'][a-zA-Z]*) in the Text to Find edit box and $0 : 5 : 2 in the Replace With edit box to achieve the same result.

We are not suggesting that you would use regular expressions with such a short program, but you want simple examples for experimenting with the possibilities. Some of you should find this an entertaining exercise if you have plenty of time and no pressure. Delphi Help provides plenty of guidance and examples. Follow this link to find out about Lazarus regular expressions.

Programming - a skill for life!

Stepping through code, setting breakpoints, error handling and range checking