Solved

Delphi, RichEdit, Find method

Posted on 2011-02-18
10
1,300 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
Comment Utility
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
Comment Utility

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
Comment Utility
Ok,
Thanks jimyX, and ewangoya:

Will get back to you
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 32

Expert Comment

by:ewangoya
Comment Utility

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
Comment Utility
I have provided a working solution to the author's question!
0
 
LVL 32

Expert Comment

by:ewangoya
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Printing problem 2 72
Modbus - whats the maximum I can store in one register? 4 74
scoreUp challenge 14 47
Delphi Form ownership 4 50
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

762 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

7 Experts available now in Live!

Get 1:1 Help Now