Solved

Delphi, RichEdit, Find method

Posted on 2011-02-18
10
1,318 Views
Last Modified: 2012-05-11
Hi,
I Have a Special requirement. I am Using Delphi 7. I am loading some text/RTF files on RichEdit Component. I am hi liting the searched Word on the RichEdit.

For Example, I am Searching for "SQL Server", it Shows the Words in RTF. My Requirement is I have to Select Not only the given Words, Also select if there is any Special symbols within the Words. like "SQL.Server","SQL-Server" etc.

Thanks and Regards,
VIJESH V.NAIR
0
Comment
Question by:OTSSolutions
10 Comments
 
LVL 24

Accepted Solution

by:
jimyX earned 250 total points
ID: 34925111
You can add all the special symbols that you expect in an array and search by replacing any space or non-alphabetic char in the phrase you want to find by substituting from that array one by one until you find and highlight all the similarities:
var // global var
  SpecialChar:array [1..3]of char = ('-','.','_'); // you can add any additional symbols

procedure TForm1.FindText(FindStr: string);
var X, ToEnd : integer;
    oldCursor : TCursor;
begin
   oldCursor := Screen.Cursor;
   Screen.Cursor := crHourglass;
   with RichEdit1 do
   begin
     X := 0;
     ToEnd := length(Text) ;
     X := FindText(FindStr, X, ToEnd, []) ;
     while X <> -1 do
     begin
     SetFocus;
     SelStart := X;
     SelLength := length(FindStr) ;
     SelAttributes.Color := clRed;
     X := FindText(FindStr,X + length(FindStr),ToEnd, []);
     end;
   end;
   Screen.Cursor := oldCursor;
end;

function SpecialSymbol(Ch:Char):Boolean;
begin
  if  Ch in ['a'..'z','A'..'Z'] then
    Result := False
  else
    Result := True;
end;

procedure TForm1.Button1Click(Sender: TObject) ;
var
   Str: string;
   i, j:integer;
begin
   Str := 'SQL Server';
   FindText(Str);

   for i:= 1 to length(Str) do
     begin
       if SpecialSymbol(Str[i]) then
         begin
           for j:= 1 to high(SpecialChar) do
             begin
               Str[i] := SpecialChar[j];
               FindText(Str);
             end;
         end;
     end;
end;

Open in new window

I am using the Search Function from:
http://delphi.about.com/cs/adptips2003/a/bltip0803_2.htm
0
 
LVL 32

Assisted Solution

by:ewangoya
ewangoya earned 250 total points
ID: 34939795

For wild card searches, you need to use regular expressions.
Download and install TRegEx or TPerlRegEx

Using it is very easy, just set the expression, in your case could be SQL?Server
The ? will represent any character
0
 

Author Comment

by:OTSSolutions
ID: 34941685
Ok,
Thanks jimyX, and ewangoya:

Will get back to you
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 32

Expert Comment

by:ewangoya
ID: 35180438

I have already pointed the Author to the correct direction using Regular Expressions which can be downloaded from
http://www.regular-expressions.info/delphi.html.
Thats the way to search using wild cards
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35224456
I have provided a working solution to the author's question!
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35237248
I will not object to this but just make a note

This is class, CS Analysis of Algorithms
You have a classic example of pattern matching here, while putting in effort to develop a solution, research should be properly done on a problem before attempting to formulate a solution.

This type of question is the exact reason why we have Regular Expressions. TPerlRegEx and TRegEx are thoroughly tested and time proven methods for solving such problems.

One of the basis of OO is code reuse so we don't have to re_invent the wheel and a solution using such loops is not forward thinking neither is it expected to work under all conditions. This is the kind of coding where you have to create new application builds everyweek because some one added another search character to a search
0
 

Expert Comment

by:South Mod
ID: 35292442
All,
 
Following an 'Objection' by jimyX (at http://www.experts-exchange.com/Q_26914116.html) to the intended closure of this question, it has been reviewed by at least one Moderator and is being closed as recommended by the Expert.
 
At this point I am going to re-start the auto-close procedure.
 
Thank you,
 
SouthMod
Community Support Moderator
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now