Regular expression in Delphi 7

Asked

Viewed 2,920 times

4

Guys, I’m new to programming on Delphi 7, and also regular expressions.

Delphi 7 because in the company I’m working at, for other reasons, they have to use Delphi 7.

I need to work with regular expressions, someone knows some way. From what I read, there’s a library for this, someone could give me a north?

  • Do you happen to use the Jedi JCL in the project? because it has the Tjclregex class to work with regular expression. We use it here in the company and serves very well!

1 answer

3

For versions prior to XE, there are alternatives like Tregexpr that is free.

In the latest versions of Delphi there are native stand, the unit (Unit) to be used is System.RegularExpressions.

A example simple to replace:

function TForm1.ReplaceCC(const Match: TMatch): string;
begin
  i := i + 1;
  Result := InttoStr(i);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  regex: TRegEx;
  input: string;
  myEval: TMatchEvaluator;
begin
  i := 0;
  input := Edit1.Text;
  regex.Create('cc');
  myEval := ReplaceCC;
  Edit2.Text := regex.Replace(input, myEval);
end;

There is also the open source library System.RegularExpressionsCore.TPerlRegEx implementing Perl compatible regular expressions.

  • Thank you colleague. What I’m looking to do is the following: I want to create in my application, an SQL finder, similar to this: www.sqlformat.org. I do not know if it is necessary to use regex, because I tried to do some things already, and it almost worked. What I did, I put 2 memos and 1 button in the form. In the button action, I take the content of the sql memo that is not indented, and play in a stringlist, and separate the words by space, something like that:

  • Part := Tstringlist.Create; Part.Delimiter := ' '; Part.Delimitedtext := query; Part.Clear; Extractstrings([' '], [], Pchar(query), Part); So if I assign the formatted sql content of memo 2 to the content of Part.text, All words are separated and stay under each other. So what I did was treat the reserved words with the case. But still, I have other difficulties, however, I don’t know if this logic is the most appropriate, what do you think?

  • @Éderpereira I think this is the same way. To get a better idea, look at the code of JEDI Code Format it is not an identator of SQL, but it gives to have a notion. On the answer there is something that I can improve in it?

  • thanks again. Actually, I’m having a hard time, because he’s generating some duplicate lines, and I don’t know how to remove them, if you want, I’ll email you, I don’t know if it’s convenient to post here. That’s fine with me.

  • @Éderpereira Well, I don’t have Delphi to test now, if you want create a new question and post the code you have so far, maybe more people can help. If there’s anything else I can put in the answer regarding regex, can say, if possible mark the answer as accepted.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.