1

Good day. I can't seem to find an example of how use the TRegExpr component to do a simple replace of invalid characters. For example i have a string = 'abcdeg3fghijk'; and i want to replace all the characters that are invalid such as the numerial '3', how would process this with TRegExpr to replace all invalid characters. My intention is learn how to use the TRegExpr to build a simple url cleaner/validator.

procedure TForm1.Button3Click(Sender: TObject);
var
  RegExp: TRegExpr;
  astr:string;
begin
  astr:='h"ttp://ww"w.msn."com~~~';
  // I want to clean the string to remove all non valid chars

  //this is where I am lost

  RegExp:=TRegExpr.Create;
  try
    RegExp.Expression:=RegExpression;  
  finally
    RegExp.Free; 
  end;
end;
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
megatr0n
  • 362
  • 4
  • 18

1 Answers1

1

Judging from the commments and the question edit, you are trying to work out how to perform a replacement using a regex. The function you need is TRegEx.Replace.

There are lots of overloads. The simplest to use are the class functions. For example:

NewValue := TRegEx.Replace(OldValue, '3', '4');

will replace all occurrences of 3 with 4.

Or if you want to use the instance method approach, do it like this:

var
  RegEx: TRegEx;
....
RegEx.Create('3');
NewValue := RegEx.Replace(OldValue, '4');

Remember that TRegEx is a record, a value type. There's no Free to call and no need for try/finally. I personally regard Create as very badly named. I would have preferred Initialize if I had been designing the TRegEx type.

Using the instance method approach allows the expression to be compiled and that speeds up performance for repeated matching of the same expression to different input data. I don't know whether that would matter for you. If not then use the class function interface which is simpler to use.

You'll obviously extend this to use a useful regex for your replacement!

The documentation for the PCRE regex flavour that Delphi uses is here: http://www.regular-expressions.info/pcre.html

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I tried it and got a DCC error Incompatible types "boolean" and "char". – megatr0n Mar 30 '13 at 08:34
  • Not with that code. The three parameters receive `string`, and the return value is a string. Nothing there attempts to match char and boolean. – David Heffernan Mar 30 '13 at 08:39
  • Couldn't agree more on the naming. Create is awful on value types. Creates (ha!) all sorts of confusion over life time management. Our convention of naming record types with an R instead of T, ie RSomeRecord instead of TSomeRecord, helps avoid it, but not much. – Marjan Venema Mar 30 '13 at 09:29
  • @MarjanVenema The other change I would make is not to have an initialiser that initialises `Self`. Much better is a class function that returns a new value, named `New`. That way you could do `TRegEx.New(Expression)` and pass that to a function that received a `TRegEx`. It makes the types so much more composable. These design decisions are really important, and it's a bad sign to see such a major area of functionality with such sloppy design. – David Heffernan Mar 30 '13 at 09:31
  • Again, couldn't agree more. The VCL/RTL seems to have been losing value on the usability/extensibility front and as an example of good practices and design. – Marjan Venema Mar 30 '13 at 09:36
  • Well, traditional `Create` is an initializer too, object instances are completely prepared and are ready to use before `Create` happens. – OnTheFly Mar 30 '13 at 20:03
  • @user539484 But `Instance.Create;`?? That's the very worst of all worlds. Passing to a procedure involves creating a local variable! – David Heffernan Mar 30 '13 at 20:04
  • IIRC, in TP they had a constructor properly named `Init` – OnTheFly Mar 30 '13 at 21:46