Solved

Delphi, RichEdit, Find method

Posted on 2011-02-18
10
1,400 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This article will show, step by step, how to integrate R code into a R Sweave document
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

839 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