We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Coping the paragraph that a certain word appears in (500 points)

rincewind666
rincewind666 asked
on
Medium Priority
293 Views
Last Modified: 2010-04-05
When a certain word appears in the text of Memo1, I want just the paragraph to be copied to Memo2.  I am using Delphi 6.  

I am giving 500 points as this code is required urgently. Many thanks for your help.
Comment
Watch Question

Author

Commented:
Almost forgot. I need ALL the paragraphs that this word appears in, not just the first.  Thanks.

Commented:
procedure TForm1.Memo1Change(Sender: TObject);
begin
Memo2.Text:=Memo1.Text;
end;
CERTIFIED EXPERT
Commented:
I would think the best way to do this is to break the paragraphs up into a TStringlist, and then iterate through the list and check each paragraph for the word.

The first issue is how to tell where a paragraph ends.  I will assume that a carriage return marks the end of a paragraph.

I added the lines below to a TMemo names mmoSrc, and 2 buttons 'Find  Frank' and 'Find Bob', the results are placed into a TMemo named mmoDest.
------------
Paragraph 1 contains the names Bob and Frank as well as other text and other
textand other textand other textand other textand other textand other textand other
text.

Paragraph 2 contains the name Bob as well as other text and other textand other
textand other textand other textand other textand other textand other text.

Paragraph 3 contains the name Frank as well as other text and other textand other
textand other textand other textand other textand other textand other text.
-----------

heres a link to a zip file containing my test project
www.certss.com\Downloads\ExpertExchange\FindParagraphsInMemo.zip

heres the source.  In order to find whole words 'bob' and not 'bobby' i pad the word with spaces as well as the paragraph.  The issue here is that if the paragraph ends in the word Bob. or Bob! or Bob, then it will not find.  You can easily fix this.  I added the check for 'Bob.' add what you like.  this should give you an idea though.

---------------

procedure TForm1.BreakIntoParagraphs(s:String;sl:TStringlist);
var
    src     : String;
    tmp     : String;
    iPos    : Integer;
begin
     sl.Clear;

    src     := s;
    iPos    := Pos(#13#10#13#10,src);
    while iPos>0 do begin
        tmp := LeftStr(src,iPos+3);
        sl.Add(tmp);
        src := RightStr(src,length(src)-iPos-3);

        iPos    := Pos(#13#10#13#10,src);
    end;
    if length(src)>0 then
        sl.Add(src);
end;

procedure TForm1.GetParagraphsWithWord(sWord:String;s:String;sl:TStringlist);
var
    sPar    : String;
    ndx     : Integer;
begin
    BreakIntoParagraphs(s,sl);

    for ndx := sl.Count-1 downto 0 do begin
        sPar := ' '+sl[ndx]+' ';
        if not (AnsiContainsText(sPar,' '+sWord+' ') or AnsiContainsText(sPar,' '+sWord+'.')) then
            sl.Delete(ndx)
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
    sl : TStringlist;
begin
    sl := TStringlist.Create;
    try
       GetParagraphsWithWord('Bob',mmoSrc.Text,sl);
       mmoDest.Lines.Assign(sl);
    finally
        sl.Free;
    end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
    sl : TStringlist;
begin
    sl := TStringlist.Create;
    try
       GetParagraphsWithWord('Frank',mmoSrc.Text,sl);
       mmoDest.Lines.Assign(sl);
    finally
        sl.Free;
    end;
end;


-----------------------------

Someone thats good with RegEx could probably do this with much less code, but I like to do things the old fashion way!  :)


Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.