PDA

View Full Version : آموزش: استخراج یک فایل متنی از یک فایل متنی دیگر با استفاده از 2 delimiter



SayeyeZohor
جمعه 09 خرداد 1393, 01:36 صبح
با سلام

من نیاز داشتم که یک رشته رو که بین 2 کوتیشن قرارداره رو پیدا کنم

یه چیزایی پیداکردم که حیفم اومد اینجا نذارم

پیشاپیش عرض کنم این کدها رو جهت استفاده دوستان می ذارم نه برای اینکه به اسم خودم تموم بشه یا ...

به فرض مثال متن ما هست 'Hi my name is$John and I'm happy/today'

(خب متن هدف ما : بین دو جداکننده $ و /) = John and I'm happy


به ترتیب function هایی که در این مورد هستند رو قرار می دم :


function ExtractText(const Str: string; const Delim1, Delim2: char): string;
var
pos1, pos2: integer;
begin
result := '';
pos1 := Pos(Delim1, Str);
pos2 := Pos(Delim2, Str);
if (pos1 > 0) and (pos2 > pos1) then
result := Copy(Str, pos1 + 1, pos2 - pos1 - 1);
end;

SayeyeZohor
جمعه 09 خرداد 1393, 01:37 صبح
function ExtractDelimitedString(const s: string): string;
var
p1, p2: Integer;
begin
p1 := Pos('$', s);
p2 := Pos('/', s);
if (p1<>0) and (p2<>0) and (p2>p1) then begin
Result := Copy(s, p1+1, p2-p1-1);
end else begin
Result := '';//delimiters not found, or in the wrong order; raise error perhaps
end;
end;



Use System.StrUtils;


function ExtractText(const Str: string; const Delim1, Delim2: string): string;
var
pos1, pos2: integer;
begin
result := '';
pos1 := Pos(Delim1, Str);
if pos1 > 0 then begin
pos2 := PosEx(Delim2, Str, pos1+1);
if pos2 > 0 then
result := Copy(Str, pos1 + 1, pos2 - pos1 - 1);
end;
end;

SayeyeZohor
جمعه 09 خرداد 1393, 01:38 صبح
:تشویق:



function ExtractText(const Str: string; const Delim1, Delim2: string): TStringList;
var
c,pos1, pos2: integer;
begin
result:=TStringList.Create;
c:=1;
pos1:=1;

while pos1>0 do
begin
pos1 := PosEx(Delim1, Str,c);
if pos1 > 0 then begin
pos2 := PosEx(Delim2, Str, pos1+1);
if pos2 > 0 then
result.Add(Copy(Str, pos1 + length(delim1), pos2 - (length(delim1) + pos1)));
c:=pos1+1;
end;

end;
end;

SayeyeZohor
جمعه 09 خرداد 1393, 01:43 صبح
procedure TForm1.Button1Click(Sender: TObject);
begin
var

names : TStringList; // Define our string list variable
ageStr : String;
i : Integer;
begin
// Define a string list object, and point our variable at it
names := TStringList.Create;


// Now add some names to our list
names.CommaText := 'Neil=45, Brian=63, Jim=22';


// And now find Brian's age
ageStr := names.Values['Brian'];


// Display this value
ShowMessage('Brians age = '+ageStr);


// Now display all name and age pair values
for i := 0 to names.Count-1 do
begin
ShowMessage(names.Names[i]+' is '+names.ValueFromIndex[i]);
end;


// Free up the list object
names.Free;
end;



Brians age is 63
Neil is 45
Brian is 63
Jim is 22






Example code : Using DelimitedText, Delimiter and QuoteChar



procedure TForm1.Button1Click(Sender: TObject);
begin
var

cars : TStringList; // Define our string list variable
i : Integer;
begin
// Define a string list object, and point our variable at it
cars := TStringList.Create;


// Now add some cars to our list - using the DelimitedText property
// with overriden control variables
cars.Delimiter := ' '; // Each list item will be blank separated
cars.QuoteChar := '|'; // And each item will be quoted with |'s
cars.DelimitedText := '|Honda Jazz| |Ford Mondeo| |Jaguar "E-type"|';


// Now display these cars
for i := 0 to cars.Count-1 do
ShowMessage(cars[i]); // cars[i] equates to cars.Strings[i]


// Free up the list object
cars.Free;
end;





Honda Jazz
Ford Mondeo
Jaguar "E-type"

hadisalahi2
جمعه 09 خرداد 1393, 12:14 عصر
داداش این تاپیک آخر فکر کنم کدهاش ناقص خورده
اگه میشه تصحیحش کن
دستت درد نکنه زکات علم رو دادی :تشویق:

SayeyeZohor
جمعه 09 خرداد 1393, 13:25 عصر
داداش این تاپیک آخر فکر کنم کدهاش ناقص خورده
اگه میشه تصحیحش کن
دستت درد نکنه زکات علم رو دادی :تشویق:


کجاش ناقصه؟

کاش همه زکاتامون رو سر موقع بدیم :متفکر::چشمک:

HosseinSaberi
جمعه 09 خرداد 1393, 22:56 عصر
سلا و خسته نباشید
هر چند فعلاً به کار من نمیاد ولی مطلب جالبیه
ممنون از زحماتتون

hadisalahi2
شنبه 10 خرداد 1393, 16:32 عصر
پست 4 و 5 مال چیه؟
مشخص نیست کدها توی پروسیجره یا باید جای دیگه بزاری

SayeyeZohor
شنبه 10 خرداد 1393, 20:57 عصر
پست 4 و 5 مال چیه؟
مشخص نیست کدها توی پروسیجره یا باید جای دیگه بزاری

عزیز جان این یه پروسیجر معمولیه ، مثلاً ButtonClick

به چه چیزایی گیر میدی؟