Text Search

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]




Search WWW

Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.


Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. 


We receive a few cents from each purchase.  Thanks


Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Mensa Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

Mensa 365 Puzzlers  Calendar 2017

Mensa 365 Puzzlers Calendar 2018

(Hint: If you can wait, current year calendars are usually on sale in January.)


Feedback:  Send an e-mail with your comments about this program (or anything else).

Search DelphiForFun.org only




Problem Description

Here is a simple program to search a text file  for a user specified string. 

Background & Techniques

The above description applies to Version 1 of the program.  Version 2.0 adds some user requested features:

bulletAbility to output to display only, to a user specified file name, or both.
bulletAbility to  output selected records, non-selected records, or both.
bulletFor matched records, ability to keep any combination  of
bulletText before the matched string
bulletThe matched string
bulletThe text following the matched string.

Notes for Programmers

Version 1 Notes

The substring search function, Pos, is used to perform the basic search of each line of the input text file.  

If the "Case sensitive" checkbox is checked, the search is performed "as is".  If "Case Sensitive" is not checked, then both the input line and the input string are converted to upper case before the "Pos" search is performed.

If "Whole words only" is checked a call is made to function IsWord   which returns true two conditions are met:  

bulletThe position before the first letter of the search string is a delimiter or 0 (i.e. the string begins at the beginning of the line). 
bulletThe position after the last letter of the search string is a delimiter or past the end of the line (i.e. the string ends at the end of the line). 

One more check box, "Show hits only" ,  allows user  to display only the lines with matches or  to display the entire input file with matched strings  highlighted.   In order to highlight the matches strings, a TRichEdit is used.  The matched strings are selected using Selstart and SeLength properties and setting SelAttributes  to make the font style bold.  The same technique could also apply color or other style properties to matches.   One trick is used  to highlight multiple occurrences of the search string in the displayed input line - each occurrence is replaced by  non-displayable characters in the working copy of the line before the next search is performed.

Version 2 Notes 

The "Show hits only" checkbox has been replaced by a number  of checkboxes to allow maximum flexibility for selecting and outputting. Nothing difficult about the coding except that there are now 9 checkboxes which means 2^9 (512) configurations to check for correct handling!   I confess to not having checked them all, but I have checked that the common ones  work as stated.  I'll count on users to report any errors that I missed.  Using mnemonic names for the boxes is a good practice to simplify testing in complex cases.    

February 20, 2014:     Version 2.1 posted today, now "remembers" from run to run the options chosen for text searching.   

May 23, 2017:  In order to assemble particular subject topics from several files, the program was modified today to append selected records to an existing text file  as an alternative to overwriting an existing file with the same name.  Version 2.2 posted today reflects the change.

Download source

Download executable



Created 17 Jan 17 2004  

Modified 11 May 2018



  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright 2000-2018, Gary Darby    All rights reserved.