Replacing and filtering text with regular expression using Delphi, Visual Basic and ASP
Posted by Daniel - 3,602 Views
A regular expression (abbreviated as regexp or regex, with plural forms regexps, regexes, or regexen) is a string that describes or matches a set of strings, according to certain syntax rules. Regular expressions are used by many text editors and utilities to search and manipulate bodies of text based on certain patterns. Many programming languages support regular expressions for string manipulation. For example, Perl and Tcl have a powerful regular expression engine built directly into their syntax. The set of utilities (including the editor sed and the filter grep) provided by Unix distributions were the first to popularize the concept of regular expressions - wikipedia.org
We are going to find out how we can use regular expression in Delphi, Visual Basic and ASP. Since regular expression is a complex package to explore, this article will only expose the replacing and filtering function in a simple manner.
DELPHI 6 - Unfortunately Delphi does not include regular expression as its built-in feature. To use regular expression within the Delphi IDE you have to use third party component. The sample on this article uses TRegExpr class library written and managed by Andrey V. Sorokin. To use TRegExpr, simply include the RegExpr unit into your project.
Below are custom Delphi functions you can use to perform RegEx replacement and validation using the functionalities provided by TRegExpr.
var objRegEx : TRegExpr;
begin
objRegEx := TRegExpr.Create;
try
objRegEx.Expression := strRegExPattern;
Result := objRegEx.Replace(strInput,strReplacement,True);
finally
objRegEx.Free;
end;
end;
VISUAL BASIC 6 - This language needs the Microsoft VBScript Regular Expression 5.0 libray or higher to perform regex pocessing. Add reference of the libray via Project - References menu to include it into your VB6 project, then use the code below as an example to perform RegEx text replacement.
Dim regEx As RegExp
Dim strOutput As String
PerformRegExReplacement = “”
Set regEx = New RegExp
regEx.Global = True
regEx.Pattern = strPattern
strOutput = regEx.Replace(strInput, strReplacement)
Set regEx = Nothing
PerformRegExReplacement = strOutput
End Function
ASP syntax is similar to the VB6 syntax, if you want to use the above function into an ASP page, all you need to do is modify the code to conform with the VBScript syntax
VISUAL BASIC.NET - Regular expression already included as a built in support in its internal library. Add an import statement for System.Text.RegularExpressions at the top of your project code like below.
And the function example is below
Dim reg_exp As New Regex(strPattern)
PerformRegexFiltering = reg_exp.Replace(strInput, “”)
End Function
NOTE: The expression language used my Microsoft is very similar to, if not identical to, the one used in Perl.
You may also check the available tested free softwares to help you learn regular expression on my other post. No guarantee that you will get better in regex with these softwares, but they surely will help :)
__________________________
The following posts are programmatically considered as related to the current post by YARPP Plugin:
- Free Softwares to help you learn Regular Expression or to enhance your RegEx skill
- Visual Basic.NET, an example of using HTTPWebRequest object
- Downloading file and extracting ZIP compressed file with Visual Basic.NET
- Visual Basic Version of ASCII-to-PDU and PDU-to-ASCII Converter Functions
- EditorKu (Multiple sheet text editor)
Related posts brought to you by Yet Another Related Posts Plugin.
Latest from Programming
- Microsoft Excel Import External Data Problem: When Microsoft Query doesn’t recognize some of your parameters
- SmartImageResizer Plugin, WordPress plugin based on Joe Lencioni’s Smart Image Resizer
- Resize Image or Crop Image with Joe Lencioni’s Smart Image Resizer, WordPress Setup
- Free Softwares to help you learn Regular Expression or to enhance your RegEx skill
- ConsoleProgressBar - Simple Progress Bar Function for your VB.Net Console Application
6 Responses to “Replacing and filtering text with regular expression using Delphi, Visual Basic and ASP”
Leave a Reply
Latest Blog Entries
- Sweet Home 3D, a free home design software
- 26x Optical Zoom on a Compact Digital Camera?
- Windows 7 Release Candidate is now available
- Microsoft Excel Import External Data Problem: When Microsoft Query doesn’t recognize some of your parameters
- The Lack of Chances is the Source of Poverty, Can we make a different?
- SmartFTP Client is no longer Free. What would be the best replacement for it?
- Kantaris Media Player, The most suitable Free Media Player on my Audio Environment
Categories
Neighbours and Friends
- Anti Teori
- Busby SEO Test Page
- Deny Sri
- Digital Media Convert
- Freeware Maniac
- Oeroek
- RidhoCyber
- Suraja Web
Comments - Thanks Guys :)
Therese Lachance: Hi, Any idea how to have ContuttoPDF fetch the correct page language?
Marlena Albu: Super Blog, Dude! I am constantly on the watch for new and interesting sites and postings about audio equipment… which is what...
tresloukadu: yo how did u fixed when the tags shows <? and it shows < “& l t ; ” ?? please send me an email.
Sean: This is a great piece of code and thanks for adding the updates. Sean’s last blog post: Not All Text Message Marketing Is Created...
rodhy: Thank for your code, it very usefull for me. best regard.
Most Popular Entries
- Passing arguments to your VB.NET console application
- ConsoleProgressBar - Simple Progress Bar Function for your VB.Net Console Application
- ASCII To PDU Converter (Convert ASCII to PDU and vice versa)
- An example: Using CPort Delphi Component to read data from your cellphone
- Microsoft Excel Import External Data Problem: When Microsoft Query doesn’t recognize some of your parameters
Software Collections
- Sweet Home 3D, a free home design software
- SmartFTP Client is no longer Free. What would be the best replacement for it?
- Kantaris Media Player, The most suitable Free Media Player on my Audio Environment
- TCPView for Windows, Tool to check TCP and UDP Connections on your system
- The Code Snippet Plugin, my favourite syntax highlighter plugin for WordPress
Blogging Related Stuff
- Change the WordPress permalink structure to the best format, and yes it’s a little bit scary :D
- Something is wrong, the Google Search Result from my blog looks bad, don’t laugh please :D
- Several tips to reduce Blog Noise, especially when your blog covers more than one niche
- How to remove your indexed pages or even your entire site from Google and Yahoo!


















February 26th, 2008 at 10:33 am
Hello, thanks for the RXTest that you attach on me..
btw, i still confuse with it..
1. how do i use it?
2. why we have to use Regex? because most from the net i read, they only explain vary global, not too specific..
3. how do we use Regex in C?
Thanks for the time..
February 27th, 2008 at 12:25 am
@littleprincess: I have no idea what OS are you familiar with but if you familiar with DOS, I am sure you know how to list files in a single directory. It’s using DIR command right? OK now consider these DIR command syntaxs:
DIR A*.TXT
DIR *.*
The 1st one will list all files with EXE as the tpye. The 2nd will list all files begin with “A” character and have TXT as the type. AB.TXT,ABC.TXT, Alexa.TXT are some file names that match with the pattern. The 3rd will list any file with any type.
Regular Expression is an extension of such capability.
_______________
To use RXTest try to enter this pattern into [Regular Expression] textbox:
(.*?)-(.*?)-(.*?)
Then enter this string into [Test String] textbox:
A-B-C
Hit the [Test] button then look at the result. Try to experiment with that. Try to enter A-B instead of A-B-C.
If you need another tool you can try Expresso from CodeProject. It’s also a good tool to test and learn RegEx.
_______________
To be honest, I don’t have any experience in C language, so I have no idea how to do it - I am sorry. But you can search it with Google, you can use this shortcut.
Good Luck
_______________
Daniel
February 27th, 2008 at 12:28 pm
Okay,no problem at all.. it has already helpfull for me.. By the way, it can be used in describing some tokens,right?
do you know any tokens in C or delphi?
Thank you very much..
February 28th, 2008 at 3:24 am
@littleprincess: I believe this is the definition of “Token” you are refering to:
In computing, a token is a categorized block of text, usually consisting of indivisible characters known as lexemes. A lexical analyser initially reads in lexemes and categorizes them according to function, giving them meaning. This assignment of meaning is known as tokenization - wikipedia.
Yes, RegEx can be used to work with Token since both of them related to text processing. And this is your Token example on Delphi
Enjoy :)
_______________
Daniel
February 29th, 2008 at 4:26 am
Daniel,
Nice piece of visual basic code. Thanks for sharing this.
Keith
February 29th, 2008 at 5:19 pm
@Keith: Right back at you Keith :)
_______________
Daniel