Delphi - extract string between tags (duplicate tags) -
i'm trying write function extract string between 2 tags. problem first tag duplicate in string unknown count e.g.
str := 'delphi app hello hello sometext here hello hello hello test!';
what want extract hello test!
- tagf last hello word
- tagl test!
the duplicate count of tagf random.
function sextractbetweentagsb(const s, lasttag, firsttag: string): string; var i, f : integer; stemp : string; begin stemp := s; repeat delete(stemp,pos(firsttag, stemp),length(firsttag)); until ansipos(firsttag,stemp) = 0; f := pos(lasttag, stemp); result:= firsttag+' '+copy(stemp, 1, length(stemp)); end;
the output is:
hello delphi app sometext here test!
function sextractbetweentagsb(const s, lasttag, firsttag: string): string; var plast,pfirst,pnextfirst : integer; begin pfirst := pos(firsttag,s); plast := pos(lasttag,s); while (plast > 0) , (pfirst > 0) begin if (pfirst > plast) // find next lasttag plast := posex(lasttag,s,plast+length(lasttag)) else begin pnextfirst := posex(firsttag,s,pfirst+length(firsttag)); if (pnextfirst = 0) or (pnextfirst > plast) begin result := copy(s,pfirst,plast-pfirst+length(lasttag)); exit; end else pfirst := pnextfirst; end; end; result := ''; end; var s: string; begin s := 'delphi app hello hello sometext here hello hello hello test! hello'; writeln(sextractbetweentagsb(s,'test','hello')); end.
output:
hello test
Comments
Post a Comment