1

In JavaScript it is possible to replace string with callback that takes matches as arguments using regular expressions.

function replacer(match, p1, p2, p3, offset, string){
  // p1 is nondigits, p2 digits, and p3 non-alphanumerics
  return [p1, p2, p3].join(' - ');
};
newString = "abc12345#$*%".replace(/([^\d]*)(\d*)([^\w]*)/, replacer);

Is there a way to do something like this in Delphi? I tried to search but did not find anything useful.

3
  • 1
    AFAIK, there is not a similar function in Delphi, but you can build your own using anonymous methods and regular expressions. Commented Nov 28, 2013 at 20:47
  • Thanks for the links. I will read that and try to do something. I hoped that Delphi has some built-in functions for that. Commented Nov 28, 2013 at 21:06
  • A bit unrelated, but fyi, [^\d] is synonymous with \D, and [^\w] with \W. Good luck! Commented Nov 29, 2013 at 0:05

1 Answer 1

2

Delphi's TPerlRegEx class has an OnReplace event handler that is called by the Replace and ReplaceAll methods for each replacement. You can use this event handler to achieve the same that you'd use a callback for in JavaScript.

In addition, the TRegEx.Replace method has overloads that take a TMatchEvaluator parameter. This type is a function reference which is used as a callback just like in JavaScript.

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

Comments

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.