Replacing and filtering text with regular expression using Delphi, Visual Basic and ASP
Posted by Daniel - 4,515 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)
6 Responses to “Replacing and filtering text with regular expression using Delphi, Visual Basic and ASP”
Leave a Reply
Hi, my name is Daniel Nugraha, a single male live on an island called Java, Indonesia. This is the place for me to share my interest in computer programming.
-
Get my Full Feed Here
Comments - Thanks Guys :)
Rangga Kusuma: Gan, Tengkiu buat postingan yang sangat berguna. Kebetulan ada project utk bikin sms gateway, dan converter Agan sangat berguna utk...
Chuck Norton: I actually went ahead & bolted over to Justin’s Get The Image plugin here: http://justintadlock.com/ar...
Chuck Norton: Question: is it possible to insert something like [custfieldimg=”joice1.jpg,15 0,1:1″] into the actual templates instead of...
Therese Lachance: Hi, Any idea how to have ContuttoPDF fetch the correct page language?
tresloukadu: yo how did u fixed when the tags shows <? and it shows < “& l t ; ” ?? please send me an email.














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