请教,在  richedit1 搜索字符串的问题?? 一个比较大的文本文件,我用  richedit1 将其打开,
我想搜索出这个文本文件中的一些特定的字符,比如说是这样的:
我想要得字符总是存在于 aaa 和 bbb  之间
例如:  .....aaaQQQbbbjkljaklsjkfdlsaaaXXXbbb.....
其中 QQQ  和 XXX 是我所需要的,原来我用的是 richedit1.FindText语句,可能是我没有用对,不太理解,请指教!!
多谢各位大哥!!!

解决方案 »

  1.   

    给一些代码供你参考:
    procedure TForm1.FormCreate(Sender: TObject);
    begin
     {make richedit edit 32+k files. MUST have 'richedit' in USES above}
     richedit.perform(EM_EXLIMITTEXT, 0, 8000000);end;procedure TForm1.openbuttonClick(Sender: TObject);
    begin
     if opendialog1.execute
        then richedit.lines.loadfromfile( opendialog1.filename);
    end;procedure TForm1.findbuttonClick(Sender: TObject);
    var
     tofind,replacewith: string;
    begin
     if Fedit.text <> '' then find(tofind,replacewith);
    end;procedure TForm1.replacebuttonClick(Sender: TObject);
    var
     tofind,replacewith: string;
    begin
     if Fedit.text <> '' then
      begin
       tofind := Fedit.text;
       replacewith := Redit.text;
       find(tofind,replacewith)
      end;
    end;procedure TForm1.replallbuttonClick(Sender: TObject);
    var
     tofind,replacewith: string;
    begin
      if Fedit.text <> '' then
       begin
        tofind := fedit.text;
        replacewith := redit.text;
        replall := true; {replall = global boolean var}
        while replall = true do find(tofind,replacewith);
       end;
    end;function TForm1.find(tofind,replacewith: string): string;
    var
     matchcase,wholeword,up,top: boolean;
     FindIn,findin2,uptofind,upfindin,upfindin2: string;
     find1s,find2s,find3s,find4s: string;
     find1,find2,find3,find4: integer;
     flen,start,holder,found,index,i: integer;
    begin
     {check status of option checkboxes}
     if mcase.checked then matchcase := true;
     if wword.checked then wholeword := true;
     if searchup.checked then up := true;
     if starttop.checked then top := true; if top = true then
      begin
       richedit.perform(em_setsel,0,0);
       starttop.checked := false;
       top := false;
      end; {Load Fedit.text as var. If replacing, replace selected text
     if it is = to Fedit.  Then load document text as string var. The
     string vars are case sensitive as strings so lowercase them if
     that option is not checked.}
     if matchcase = true then
      begin
       ToFind := Fedit.text;
       if lowercase(richedit.seltext) = lowercase(tofind) then
         if replacewith <> '' then richedit.seltext := replacewith;
       findin := richedit.text;
      end
     else
      begin
       ToFind := lowercase(fedit.text);
       if replacewith <> '' then replacewith := lowercase(replacewith);
       if lowercase(richedit.seltext) = lowercase(tofind) then
         if replacewith <> '' then richedit.seltext := replacewith;
       findin := lowercase(richedit.text);
      end;
     flen := length(tofind); found := 0;
     index := -1;
     if up = true then
     {reverse contents of fedit and richedit. Perform pos() search
     for reversed fedit. It's found value is then reversed so that the
     visible richedit can use it as an em_selstart value (em_selstart
     because 'selstart :=' gets funky after 32k and 64k)}
      begin
       start := richedit.selstart;
       holder := start +1;
       for i := 0 to length(tofind)-1 do appendstr(uptofind, copy(tofind,length(tofind)-i,1));
       while found = 0 do
       begin
        if holder < -250 then
         begin
          showmessage('"'+tofind+'" cannot be found');
          replall := false; {replall = global boolean var}
          activecontrol := richedit;
          exit;
         end;
        if holder < 250 then i := holder -1
         else
          begin
           i := 250;
          end;
        {add spaces to ends of searchin string for wholeword search}
        findin2 := concat('  ',copy(findin,holder-i,i),'  ');{grab 250 chunks to search}
        upfindin2 := '';
        for i := 0 to length(findin2)-1 do appendstr(upfindin2, copy(findin2,length(findin2)-i,1));
           if wholeword = true then
            begin
             find1s := ' ' + uptofind + ' ';
             find2s := #13 + uptofind + ' ';
             find3s := #13 + uptofind + #10;
             find4s := ' ' + uptofind + #10;
             find1 := pos(find1s, upfindin2); {find each type of wholeword}
             find2 := pos(find2s, upfindin2);
             find3 := pos(find3s, upfindin2);
             find4 := pos(find4s, upfindin2);
             if find1 = 0 then find1 := 300;  {change notfound for next test}
             if find2 = 0 then find2 := 300;
             if find3 = 0 then find3 := 300;
             if find4 = 0 then find4 := 300;
             {find the lowest found value (first found) and set 'found' to it}
             if (find1 <300) and (find1 <find2) and (find1 <find3) and (find1 <find4)
                then found := find1 +1;
             if (find2 <300) and (find2 <find1) and (find2 <find3) and (find2 <find4)
                then found := find2 +1;
             if (find3 <300) and (find3 <find1) and (find3 <find2) and (find3 <find4)
                then found := find3 +1;
             if (find4 <300) and (find4 <find1) and (find4 <find2) and (find4 <find3)
                then found := find4 +1;
            end
           else found := pos(uptofind, upfindin2); {not wholeword search}
             holder := holder - 225; {225 to allow for possible truncation}
             index := index +1;
          end;
         if index > 0 then index := index *225 else index := 0;
         holder := start -index -found +3;
         ActiveControl := richedit;
         richedit.perform(em_setsel, holder-flen, holder);
         richedit.perform(em_scrollcaret, 0,0);
       end
     else {search up is not checked}
      begin
       start := richedit.selstart;
       if lowercase(richedit.seltext) = lowercase(tofind) then
         richedit.perform(em_setsel, start +flen, start +flen);
       start := richedit.selstart;
       holder := start +1;
       while found = 0 do
       begin
        if holder > (richedit.gettextlen)+223 then
         begin
          showmessage('"'+tofind+'" cannot be found');
          replall := false; {replall = global boolean var}
          ActiveControl := richedit;
          exit;
         end;
        findin2 := concat('  ',copy(findin,holder,250),'  ');
           if wholeword = true then
            begin
             find1s := ' ' + tofind + ' ';
             find2s := #10 + tofind + ' ';
             find3s := #10 + tofind + #13;
             find4s := ' ' + tofind + #13;
             find1 := pos(find1s, findin2);
             find2 := pos(find2s, findin2);
             find3 := pos(find3s, findin2);
             find4 := pos(find4s, findin2);
             if find1 = 0 then find1 := 300;
             if find2 = 0 then find2 := 300;
             if find3 = 0 then find3 := 300;
             if find4 = 0 then find4 := 300;
             if (find1 <300) and (find1 <find2) and (find1 <find3) and (find1 <find4)
                then found := find1 +1;
             if (find2 <300) and (find2 <find1) and (find2 <find3) and (find2 <find4)
                then found := find2 +1;
             if (find3 <300) and (find3 <find1) and (find3 <find2) and (find3 <find4)
                then found := find3 +1;
             if (find4 <300) and (find4 <find1) and (find4 <find2) and (find4 <find3)
                then found := find4 +1;
            end
           else found := pos(tofind, findin2);
             holder := holder + 225;
             index := index +1;
          end;
         if index > 0 then index := index *225 else index := 0;
         holder := start +index +found-3;
         ActiveControl := richedit;
         richedit.perform(em_setsel, holder, holder +flen);
         richedit.perform(em_scrollcaret, 0,0);
       end;
    end;procedure TForm1.ExitbuttonClick(Sender: TObject);
    begin
     close;
    end;
    end.
    有问题请发信息到我的E-mail:[email protected]
      

  2.   

    给个例子你:
    这个例子使用了一个TRichEdit, 一个TButton和一个TFindDialog
    从TFindDialog中取得一个字符,并在TRichEdit中查找。procedure TForm1.Button1Click(Sender: TObject);begin
      FindDialog1.Position := Point(RichEdit1.Left + RichEdit1.Width, RichEdit1.Top);
      FindDialog1.Execute;
    end;procedure TForm1.FindDialog1Find(Sender: TObject);
    var
      FoundAt: LongInt;
      StartPos, ToEnd: Integer;
    begin
      with RichEdit1 do
      begin
        if SelLength <> 0 then
          StartPos := SelStart + SelLength;
        else
          StartPos := 0;
        ToEnd := Length(Text) - StartPos;
        FoundAt := FindText(FindDialog1.FindText, StartPos, ToEnd, [stMatchCase]);
        if FoundAt <> -1 then
        begin
          SetFocus;
          SelStart := FoundAt;
          SelLength := Length(FindDialog1.FindText);
        end;
      end;
    end;
      

  3.   

    307(冲击波) , Wally_wu(韦利)  先谢谢一下,
      

  4.   


    n:=pos('aaa',richedit1.Text);
    m:=pos('bbb',richedit1.Text);用 LeftStr 函数,即可