Recent

Author Topic: Find text  (Read 5329 times)

howardpc

  • Hero Member
  • *****
  • Posts: 2833
Re: Find text
« Reply #15 on: April 10, 2018, 09:26:58 pm »
@howardpc:
Little misunderstanding.
Here are the keyWords, these are known in advance:
Code: Pascal  [Select]
  1. keyWords: array [0..3] of string = ('int', 'double', 'string','bool');
wantedWords follow the keyWords (see the picture in post#1).

I adapted my example, see attachment.

lainz

  • Hero Member
  • *****
  • Posts: 2915
    • Home
Re: Find text
« Reply #16 on: April 10, 2018, 09:27:35 pm »
That's encouraging! Compliments!!
Now find out to write it in three lines.... :P 8-)

Or a single line in JavaScript  :D

(for those don't get it, JS is just text, and can be shown all in a single line)

Really good job  :)

justnewbie

  • Full Member
  • ***
  • Posts: 135
Re: Find text
« Reply #17 on: April 10, 2018, 09:29:18 pm »
@howardpc:
Little misunderstanding.
Here are the keyWords, these are known in advance:
Code: Pascal  [Select]
  1. keyWords: array [0..3] of string = ('int', 'double', 'string','bool');
wantedWords follow the keyWords (see the picture in post#1).

I adapted my example, see attachment.
Thank you again, I will study it!

justnewbie

  • Full Member
  • ***
  • Posts: 135
Re: Find text
« Reply #18 on: April 10, 2018, 09:30:02 pm »
This version filters the repetitions:
Code: Pascal  [Select]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. const
  3.   crlf = #13#10;
  4. var
  5.   i, j: integer;
  6.   actIdx: integer;
  7.   wText: string = '';
  8.   actWord: string;
  9. begin
  10.   Memo2.Lines.Clear;
  11.   for i := 0 to Memo1.Lines.Count - 1 do
  12.   begin
  13.     actIdx := 1;
  14.     while ExtractWord(actIdx, Memo1.Lines[i], myWordDelims) <> '' do
  15.     begin
  16.       for j := low(keyWords) to high(keyWords) do
  17.       begin
  18.         if ExtractWord(actIdx, Memo1.Lines[i], myWordDelims) = keyWords[j] then
  19.         begin
  20.           actWord := ExtractWord(actIdx + 1, Memo1.Lines[i], myWordDelims);
  21.           if IsWordPresent(actWord, wText, myWordDelims) = False then
  22.             wText := wText + actWord + crlf;
  23.         end;
  24.       end;
  25.       Inc(actIdx);
  26.     end;
  27.     Memo2.Text := wText;
  28.   end;
  29.   Label1.Caption := 'Found: ' + IntToStr(Memo2.Lines.Count);
  30. end;
« Last Edit: April 10, 2018, 09:39:33 pm by justnewbie »

justnewbie

  • Full Member
  • ***
  • Posts: 135
Re: Find text
« Reply #19 on: April 10, 2018, 10:13:19 pm »
This version uses "protected words", these words cannot be placed on the list.
I'm totally sure that this code can be shorter and faster, but I'm really happy with it.  :)
Code: Pascal  [Select]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. const
  3.   crlf = #13#10;
  4. var
  5.   i, j, k: integer;
  6.   actIdx: integer = 1;
  7.   wText: string = '';
  8.   actWord: string;
  9.   newWord: boolean;
  10. begin
  11.   Memo2.Lines.Clear;
  12.   for i := 0 to Memo1.Lines.Count - 1 do
  13.   begin
  14.     actIdx := 1;
  15.     while ExtractWord(actIdx, Memo1.Lines[i], myWordDelims) <> '' do
  16.     begin
  17.       for j := low(keyWords) to high(keyWords) do
  18.       begin
  19.         if ExtractWord(actIdx, Memo1.Lines[i], myWordDelims) = keyWords[j] then
  20.         begin
  21.           actWord := ExtractWord(actIdx + 1, Memo1.Lines[i], myWordDelims);
  22.           newWord := True;
  23.           if IsWordPresent(actWord, wText, myWordDelims) = False then
  24.           begin
  25.             for k := low(protectedWords) to high(protectedWords) do
  26.             begin
  27.               if actWord = protectedWords[k] then
  28.               begin
  29.                 newWord := False;
  30.                 break;
  31.               end;
  32.             end;
  33.             if newWord then
  34.               wText := wText + actWord + crlf;
  35.           end;
  36.         end;
  37.       end;
  38.       Inc(actIdx);
  39.     end;
  40.     Memo2.Text := wText;
  41.   end;
  42.   Label1.Caption := 'Found: ' + IntToStr(Memo2.Lines.Count);
  43. end;

justnewbie

  • Full Member
  • ***
  • Posts: 135
Re: Find text
« Reply #20 on: April 10, 2018, 10:45:02 pm »
If I have an array like this:
Code: Pascal  [Select]
  1. keyWords: array [0..3] of string = ('int', 'double', 'string', 'bool');
how can I add new values to it from a TEdit component during running?

lainz

  • Hero Member
  • *****
  • Posts: 2915
    • Home
Re: Find text
« Reply #21 on: April 11, 2018, 03:09:08 am »
If I have an array like this:
Code: Pascal  [Select]
  1. keyWords: array [0..3] of string = ('int', 'double', 'string', 'bool');
how can I add new values to it from a TEdit component during running?

Don't set it 0...3, create an array with no size, something like

keyWords: array of string;

Then you can

SetLength(keyWords, Length(keyWords) + 1) when adding a new keyword

Or even better, don't use array and use TStringList, that manages the memory automatically.

ASerge

  • Hero Member
  • *****
  • Posts: 998
Re: Find text
« Reply #22 on: April 11, 2018, 07:30:22 pm »
Is there anyone who got this challenge?  :)
(Attached the text file and the example picture.)
Code: Pascal  [Select]
  1. uses RegExpr;
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. var
  5.   R: TRegExpr;
  6. begin
  7.   R := TRegExpr.Create('(int|double|bool|string)\s+(\w+)');
  8.   try
  9.     if R.Exec(Memo1.Text) then
  10.       repeat
  11.         if R.SubExprMatchCount >= 2 then
  12.           Memo2.Append(Format('%d:%s', [R.MatchPos[2], R.Match[2]]));
  13.       until not R.ExecNext;
  14.   finally
  15.     R.Free;
  16.   end;
  17. end;

justnewbie

  • Full Member
  • ***
  • Posts: 135
Re: Find text
« Reply #23 on: April 11, 2018, 08:30:18 pm »
Is there anyone who got this challenge?  :)
(Attached the text file and the example picture.)
Code: Pascal  [Select]
  1. uses RegExpr;
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. var
  5.   R: TRegExpr;
  6. begin
  7.   R := TRegExpr.Create('(int|double|bool|string)\s+(\w+)');
  8.   try
  9.     if R.Exec(Memo1.Text) then
  10.       repeat
  11.         if R.SubExprMatchCount >= 2 then
  12.           Memo2.Append(Format('%d:%s', [R.MatchPos[2], R.Match[2]]));
  13.       until not R.ExecNext;
  14.   finally
  15.     R.Free;
  16.   end;
  17. end;

Uhhhhhhhh ... it is incredible! What a code!
OK, let me ask these: can you show me a version of this code that works as originally, but additionally
1./ is able to find Var1, Var2 and Var3 in the first line? AND
2./ ignores the MyFunction in the second line?

Code: Pascal  [Select]
  1. int Var1,Var2, Var3=5;
  2. bool MyFunction(int someVar);


lainz

  • Hero Member
  • *****
  • Posts: 2915
    • Home
Re: Find text
« Reply #24 on: April 11, 2018, 09:39:49 pm »
You're making an interpreter? Or making a tool that analyze code for some reason? A tool that analyze code and edit it?

engkin

  • Hero Member
  • *****
  • Posts: 2129
Re: Find text
« Reply #25 on: April 11, 2018, 09:59:46 pm »
OK, let me ask these: can you show me a version of this code that works as originally, but additionally
1./ is able to find Var1, Var2 and Var3 in the first line? AND
2./ ignores the MyFunction in the second line?

Code: Pascal  [Select]
  1. int Var1,Var2, Var3=5;
  2. bool MyFunction(int someVar);
Don't forget the same text between quotation marks should be ignored:
int FindThisVar;
someProc("int MissThisVar;");

ASerge

  • Hero Member
  • *****
  • Posts: 998
Re: Find text
« Reply #26 on: April 11, 2018, 10:38:35 pm »
OK, let me ask these: can you show me a version of this code that works as originally, but additionally...
Maybe try it yourself?
Read: https://www.regular-expressions.info/quickstart.html.
Test: https://regex101.com/.

justnewbie

  • Full Member
  • ***
  • Posts: 135
Re: Find text
« Reply #27 on: April 11, 2018, 10:43:11 pm »
OK, let me ask these: can you show me a version of this code that works as originally, but additionally...
Maybe try it yourself?
Read: https://www.regular-expressions.info/quickstart.html.
Test: https://regex101.com/.
Thank you for the links, I will study them!

justnewbie

  • Full Member
  • ***
  • Posts: 135
Re: Find text
« Reply #28 on: April 11, 2018, 10:43:45 pm »
You're making an interpreter? Or making a tool that analyze code for some reason? A tool that analyze code and edit it?
No, yes, yes.  :)

justnewbie

  • Full Member
  • ***
  • Posts: 135
Re: Find text
« Reply #29 on: April 11, 2018, 10:48:15 pm »
Can you show me a string replace example code (case sensitive and only whole words)?
Currently I can find the given word by using:
Code: Pascal  [Select]
  1. ExtractWord(actIdx, Memo2.Lines[i], myWordDelims)
I want to replace  this word with an other.