* * *

Author Topic: Find text  (Read 2585 times)

justnewbie

  • Full Member
  • ***
  • Posts: 129
Re: Find text
« Reply #30 on: April 16, 2018, 11:00:44 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/.
I cannot get the proper regex for the case when
1.) there are no spaces between words, only a comma    >> Example: int var1,var2
OR
2.) there are unknown number of spaces and a comma   >> Example: int var1 ,    var2

I need the formule that matches both of them.
For the 2.) I can use this
(int|double|string|bool)\s+(\w+)(\s+,\s+)(\w+)
but it doesnt work with 1.)

Can you help me?
« Last Edit: April 16, 2018, 11:04:14 pm by justnewbie »

justnewbie

  • Full Member
  • ***
  • Posts: 129
Re: Find text
« Reply #31 on: April 20, 2018, 04:55:55 pm »
I want to replace words in my text.
I'm using ReplaceStr, but it is not good, because it replaces all occurrences of the search string with the replacement string.
I want to replace ONLY the whole words.
How can I do it?

howardpc

  • Hero Member
  • *****
  • Posts: 2669
Re: Find text
« Reply #32 on: April 20, 2018, 09:10:50 pm »
The best solution is probably learning to master regular expressions.
However, it can be instructive to dabble at parsing strings yourself.

For instance, you could adapt the following (which may well contain bugs)  to your needs:
Code: Pascal  [Select]
  1. procedure ReplaceWholeWord(const aText, aWordToFind, theReplacement: String;
  2.   out ReplacedText: String; caseInsensitive: Boolean=False;
  3.   aSeparators: TSysCharSet=[' ',',','.',';','?',':','(',')',#10,#13]);
  4. var
  5.   p, replaceStart, lastReplaceEnd: Integer;
  6.   toFind: String;
  7.  
  8.   function GetNextWord(out aStart: Integer): String;
  9.   begin
  10.     Result := '';
  11.     while (p < Length(aText)) and (aText[p] in aSeparators) do
  12.       Inc(p);
  13.     aStart := p;
  14.     while (p < Length(aText)) and not (aText[p] in aSeparators) do
  15.       Inc(p);
  16.     Result := Copy(aText, aStart, p - aStart);
  17.     if caseInsensitive then
  18.       Result := LowerCase(Result);
  19.   end;
  20.  
  21. begin
  22.   ReplacedText := '';
  23.   if aText.IsEmpty or aWordToFind.IsEmpty or theReplacement.IsEmpty then
  24.     Exit;
  25.   p := 1;
  26.   lastReplaceEnd := 1;
  27.   if caseInsensitive then
  28.     toFind := LowerCase(aWordToFind)
  29.   else toFind := aWordToFind;
  30.   repeat
  31.     if GetNextWord(replaceStart) = toFind then
  32.       case replaceStart of
  33.         1: begin
  34.              ReplacedText := theReplacement;
  35.              lastReplaceEnd := Succ(Length(aWordToFind));
  36.            end;
  37.         else begin
  38.           AppendStr(ReplacedText, Copy(aText, lastReplaceEnd, replaceStart-lastReplaceEnd) + theReplacement);
  39.           lastReplaceEnd := p;
  40.         end;
  41.       end;
  42.   until p >= Length(aText);
  43.  
  44.   if lastReplaceEnd < p then
  45.     AppendStr(ReplacedText, Copy(aText, lastReplaceEnd, MaxInt));
  46. end;

justnewbie

  • Full Member
  • ***
  • Posts: 129
Re: Find text
« Reply #33 on: April 20, 2018, 11:59:00 pm »
@howardpc: thank you, I will study it!
Meanwhile I could do my own solution by using StuffString http://lazarus-ccr.sourceforge.net/docs/rtl/strutils/stuffstring.html

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 698
Re: Find text
« Reply #34 on: April 23, 2018, 05:30:10 am »
hello,
The best solution is probably learning to master regular expressions.
maybe with the help of the ReplaceRegExpr function from the Regexpr unit :
example to replace all the myint strings by yourint in this text :
Quote
extern int myint;
int           myint;
int      myint=1;
int        myint       = 30;
    int    myint;
int myint, myotherint=8;
for(int i=myint; ...
double dbl;int myint;
code :
Code: Pascal  [Select]
  1. procedure TForm1.Bt_RegexprClick(Sender: TObject);
  2. var
  3.   NewStr : String;
  4.   pattern : String;
  5. begin
  6.   MemConsole.Lines.LoadFromFile(TextFileName);
  7.   pattern := '(=|,|int|double|string|bool)\s*(myint)';
  8.   NewStr := ReplaceRegExpr(pattern,MemConsole.Lines.Text,'$1 yourint',true);
  9.   MemConsole.Lines.Append(' ==== After ====');
  10.   MemConsole.Lines.Append(NewStr);
  11. end;

pattern  search first a keyword = or , or int ...   followed by 0 space or more  followed by myint.
$1 yourint in the ReplaceRegExpr : $1 leaves the first part of the pattern like the original string

Result in Attachment.

Not perfect :    i=myint;  becomes i= yourint;

Friendly, J.P
« Last Edit: April 23, 2018, 05:38:59 am by Jurassic Pork »

justnewbie

  • Full Member
  • ***
  • Posts: 129
Re: Find text
« Reply #35 on: April 23, 2018, 09:41:22 am »
@howardpc and @Jurassic Pork: guys, thank you very much!  :)

justnewbie

  • Full Member
  • ***
  • Posts: 129
Re: Find text
« Reply #36 on: April 24, 2018, 01:29:40 pm »
Currently I'm using this method to check whether an item exists on my string list:
Code: Pascal  [Select]
  1. IsWordPresent(item, foundList.Text, myWordDelims)

Is there any faster method to do this?

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus