Recent

Author Topic: pos('is',fs) String functiions help  (Read 3734 times)

Peterkl

  • New Member
  • *
  • Posts: 22
pos('is',fs) String functiions help
« on: December 29, 2015, 05:19:06 pm »
Hello guys,

I've been trying to find a specific sub-string within a string, with the "pos" command. It works fine. But there's only one problem that I haven't been able to solve for the moment.  Take a look at my simple code : (the phrase of the txt file is :" this is only a text to see if...")


var
  ft :text ;
  fs :string;
begin
  assign(ft, 'c:\users\peter\desktop\file3.txt');
  reset(ft);
  repeat
   readln(ft,fs);
  until eof (ft);
  writeln(pos('is',fs));
  close(ft);
  readln;
end.         

The output of the program is "3" which is quite normal because the "this" word has the "is" word starting in the 3th position. The question is how can I search for single alone words ? In this case the position of the word would be  5 and not 3.

Cheers

balazsszekely

  • Guest
Re: pos('is',fs) String functiions help
« Reply #1 on: December 29, 2015, 05:25:23 pm »
Search for " is " instead of "is".

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12536
  • FPC developer.
Re: pos('is',fs) String functiions help
« Reply #2 on: December 29, 2015, 05:50:43 pm »
That will fail on 'Whatever this is, it sucks'  because of the comma. Similarly with other signs at the end of a sentence like ! and .

So you will have to split on words yourself. If you need to support unicode to identify words that is even harder (but not needed for 'is');



balazsszekely

  • Guest
Re: pos('is',fs) String functiions help
« Reply #3 on: December 29, 2015, 06:17:30 pm »
@marcov
You're right! My solution is not perfect by far, but to handle all possible scenarios, it would be a tremendous task. Perhaps the easiest way to achieve this is to search for " is" then check if the following char is a " ",  "!",  "." ,  ",", etc..

fic

  • New Member
  • *
  • Posts: 14
Re: pos('is',fs) String functiions help
« Reply #4 on: December 29, 2015, 06:29:04 pm »
Maybe you can use a StringReplace?
i.e. StringReplace(str,' is','/' ,[rfReplaceAll])?
Mind you : ' is' comes with a space in front, and then continue your search?

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: pos('is',fs) String functiions help
« Reply #5 on: December 29, 2015, 07:34:46 pm »
A regular expression \bis\b?

Code: Pascal  [Select][+][-]
  1. program project1;
  2. {$mode objfpc}{$H+}
  3. uses
  4.   RegExpr;
  5.  
  6. var
  7.   s, u: string;
  8.   re: TRegExpr;
  9.  
  10. begin
  11.   re := TRegExpr.Create;
  12.   try
  13.     re.Expression := '(\bis\b)';
  14.     s := 'this is only a text to see if...is,is:isis"is"';
  15.     SetLength(u, Length(s));
  16.     FillChar(u[1], Length(s), ' ');
  17.     if re.Exec(s) then
  18.       repeat
  19.         u[re.MatchPos[0]] := '-';
  20.         WriteLn(re.MatchPos[0]);
  21.       until not re.ExecNext;
  22.   finally
  23.     re.Free;
  24.   end;
  25.   WriteLn(s);
  26.   WriteLn(u);
  27.   ReadLn;
  28. end.
  29.  

balazsszekely

  • Guest
Re: pos('is',fs) String functiions help
« Reply #6 on: December 29, 2015, 08:45:02 pm »
@engkin
That's an excellent idea! I think the OP should use this solution.

Peterkl

  • New Member
  • *
  • Posts: 22
Re: pos('is',fs) String functiions help
« Reply #7 on: December 30, 2015, 12:10:28 am »
A regular expression \bis\b?

Code: Pascal  [Select][+][-]
  1. program project1;
  2. {$mode objfpc}{$H+}
  3. uses
  4.   RegExpr;
  5.  
  6. var
  7.   s, u: string;
  8.   re: TRegExpr;
  9.  
  10. begin
  11.   re := TRegExpr.Create;
  12.   try
  13.     re.Expression := '(\bis\b)';
  14.     s := 'this is only a text to see if...is,is:isis"is"';
  15.     SetLength(u, Length(s));
  16.     FillChar(u[1], Length(s), ' ');
  17.     if re.Exec(s) then
  18.       repeat
  19.         u[re.MatchPos[0]] := '-';
  20.         WriteLn(re.MatchPos[0]);
  21.       until not re.ExecNext;
  22.   finally
  23.     re.Free;
  24.   end;
  25.   WriteLn(s);
  26.   WriteLn(u);
  27.   ReadLn;
  28. end.
  29.  

Thank you m8, and thank you for the rest of you guys as well !

magu

  • New Member
  • *
  • Posts: 37
Re: pos('is',fs) String functiions help
« Reply #8 on: December 30, 2015, 01:05:28 am »
You may want to try using the following functions from the unit: strutils

ExtractWordPos
WordPosition

Both functions allow you to define delimiters for words (eg ' ', ',') and come with a predefined set of delimters (StdWordDelims)
StdWordDelims = [#0..' ', ',', '.', ';', '/', '\', ':', '''', '"', '`'] + Brackets;

Hope this helps

 

TinyPortal © 2005-2018