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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Taking Strings out of a text

Status
Not open for further replies.

RobPouwelse

Technical User
Nov 19, 2001
77
NL
Hi, i'm a newbi to Delphi (i know a bit TP and C++, you know, standart stuff)

I need to extract certain parts from a text (repeatative text) and put it in a database.. (The database part i'll worry about later) But i haven't found a way to "search" the text file for every occurance of the word "from=" and then take the value of the string that's behind it.

like this:

msgid=<1044345>, size=14065, from=<Pipo@clown.nl>, to=<rob@clown.nl>

i need to get the value's (without the &quot;<&quot; and &quot;>&quot;)..
i really haven't the foggiest on how to do it.

thanks in advance,
Rob Pouwelse
 
I'm assuming you know the difference between strings and null-terminated strings ie pChar. If not tell me.

Straight from the Delphi help:

Returns a pointer to the first occurrence of STR2 in STR1.

Unit
SysUtils

Category
string handling routines (null-terminated)

function StrPos(const Str1, Str2: PChar): PChar;

Description

StrPos returns a pointer to the first occurrence of Str2 in Str1. If Str2 does not occur in Str1, StrPos returns nil.

Note: If the source string contains international characters, use AnsiStrPos instead.

A hint: you can startup the Delphi help go to the Visual Component Library reference go to Categorical routines listing and search whatever you need.
 
The difference between a string and a null-terminated string is that the null-terminated string is terminated by a &quot;0&quot; or #0. (figures).. If i'm wrong, interrupt me on the way :)

What i don't get is that i treid a test of the AnsiStrPos but it gives an error:&quot;raised exception class EResNotFound with message 'Resouce TForm1 not found'

Told you i was a newbi :))

the program i used was :

procedure TForm1.Button1Click(Sender: TObject);
var S1 : PChar;
S2 : PChar;
S3 : PChar;

begin
S1:='String1, String2';
S2:='String2';
S3:=AnsiStrPos(S1,S2);
Label1.Caption:=S1;
Label2.Caption:=S3;
end;

If i'm right it should return either 'String2' or the position which it starts. I guess i just don't know the difference between the PChar and String (cause strings wouldn't work).

Well, if you know what i did wrong, please tell me how to correct it (preferably with an example that already works, cause otherwise i'll just screw up... again :) )

tnx!
 
First of all sorry. Before I gave you the wrong example with StrPos. Ideally, you should use the type 'string' for most of your text manipulation and only use 'pChar' for use with Windows API calls. As a newbie, you will probably appreciate strings easier usage anyway. You should be able to change the types in the var section of your code above to string and it still should work fine. You'll also have to change AnsiStrPos to something that takes string parameters such as Pos. Sorry again for the error.

As for your error all I can say is that it has nothing to do with the code. Looking at your code, everythings seems to be working. If your interested, you could just type in EResError in Delphi and put your cursor on it and press F1 which will bring up info on it.

Here's a start on what you wanted, there is some light work needed but more to do with algorithmic than Delphi code:

var S1 : string;
S2 : string;
S3 : string;

index: integer; {variable needed in copy operation}
strLength: integer; {the length of the word between the from}

begin
S1:='msgid=<1044345>, size=14065, from=<Pipo@clown.nl>, to=<rob@clown.nl>';
S2:='from';

index := pos( s2, s1 );
while index <> 0 do {if = 0 then no more occurences of from}
begin
strLength := pos( '>', s1 ) - index; {NOTE: AT THE MOMENT THIS DOES NOT WORK BECAUSE ALL IT DOES IS GET THE FIRST > IE. THE ONE OAFTER MSGID AND SUBTRACT THAT FROM INDEX WHICH IS NOT WHAT WE WANTED. WE WANT THE > FROM THE 'FROM' THAT WE'RE INTERESTED IN}
s3 := copy( s1, index+1, strLength ) {index+1 so not copy <}
end;
end;
 
There seems to be a flaw in your program.. (i think)

you state: while index <> 0 do (so if index is not 0 then do)
but in the do statement Index doesn't change, which would result in an endless loop wouldn't it?? (at least, thats what happen to me :))

my guess is that the while do statement should be a if then statement..

btw: you've been a great help, i finally know why i got that EresError, it was because i deleted the {$R *.DFM} lol! :)

tnx, if i've got any question i'll just make another thread.
 
I did some tinkering and i got this, now he also will find the right &quot;>&quot; (cause i narrowed the search)




var S1 : string;
S2 : string;
S3 : string;

index: integer;
strLength: integer;

begin
S1:='msgid=<1044345>, size=14065, from=<Pipo@clown.nl>, to=<rob@clown.nl>';
S2:='from';
index := pos( s2, s1 ) + 5; {+5 otherwise he would start from &quot;f&quot; instead of &quot;=&quot;}

if index <> 0 then
begin
strLength := pos( '>, to', s1 ) - index; {narrowed the search cause of the >, to}
s3 := copy( s1, index+1, strLength-1 );
end;
end;

tnx for giving me the info i needed, TNX!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top