Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

newbi in delphi » select text

Status
Not open for further replies.

jamesp0tter

Programmer
Feb 20, 2003
119
PT
well, i'm kind of starting in delphi... (1 month +/-)
i have some code that allows me to search a memo for some string, select it if found and copy it to other memo.

like:
[memo1.lines]
this is line one ehe
this is line two ehe
[/]

so, the only thing that changes is the 'one' / 'two'..
now imagine that that 'one' can be anything as well as the 'two' and so on... how do i select that string ? like:

[memo1.lines]
this is something that changes -» <select and copy this> «- and it ends here
[/]

any help ?

in php it would be like this:
$bla = preg_replace(&quot;'starts here (.*?) ends here.'&quot;,&quot;\$1&quot;,$ble);
just to help u understand if u code php :]

TIA !
 
Welcome to Delphi, James.

You won't find Delphi as consise as PHP especially when it comes to regular expressions.

The Delphi string functions that you will find useful are LeftStr, RightStr, MidStr and possibly Pos.

If I was coding a function to do what I think you want it would look something like this:
Code:
function GetMidString ( strList: TStringList; const left, right: string ): string;
var
 n: integer;
 line: string;
 LenL: integer;
 LenR: integer;
begin
 result := '';            
 LenL := Length(left);
 LenR := Length(right);
 for n := 0 to strList.count - 1 do begin
  line := strList[n];
  if (LeftStr(line,LenL)=Left) and (RightStr(line,LenR)=Right) then begin
   result := MidStr(LenL+1,Length(line)-LenL-LenR);
   break;
  end;
end;

I haven't tested this but it should put you on the right track.

Andrew
 
first of all thank you for your help :]

after putting your code in my app and trying to execute it, it outputs:
Code:
[Error] Unit1.pas(36): There is no overloaded version of 'MidStr' that can be called with these arguments
in
Code:
result := MidStr(LenL+1,Length(line)-LenL-LenR);
i really don't know what this is, i searched help and it turned out that that error is triggered when there are two functions with same name and vars... well i don't see them :p
what do i do ? :]

tks !

 
Whoops, my mistake.

it should be
Code:
result := MidStr(Line,LenL+1,Length(line)-LenL-LenR);
I should really test the code before replying. Sorry.

Andrew

 
ehe :] tks VERY MUCH :D
it works ;]

jamesp0tter,
jamespotter@netcabo.pt

p.s.: sorry for my (sometimes) bad english :p
 
hmmm...

i was trying to understand the code and what it does is to get the first and last word of a string, then if it matches the one i'm searching for it returns the middle string, having to know the first and last word's size to be able to subtract it and start somewhere :p

what if the string isn't 'that' simple.. like:
'bla bla a this can change lots and have more or less words : <a href=&quot;blabla.html&quot;> and this can change even more'

imagine i want to get that 'blabla.html'.. it wouldn't be that simple :|







jamesp0tter,
jamespotter@netcabo.pt

p.s.: sorry for my (sometimes) bad english :p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top