Replacing and filtering text with regular expression using Delphi, Visual Basic and ASP
Posted by Daniel - 8,583 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)
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
Popular Entries
- Passing arguments to your VB.NET console application
- Microsoft Excel Import External Data Problem: When Microsoft Query doesn’t recognize some of your parameters
- Resize Image or Crop Image with Joe Lencioni’s Smart Image Resizer, WordPress Setup
- How to Get User Input and allowing more than 256 characters to be entered on .NET Console Application
- ASCII To PDU Converter (Convert ASCII to PDU and vice versa)
- ConsoleProgressBar - Simple Progress Bar Function for your VB.Net Console Application
- An example: Using CPort Delphi Component to read data from your cellphone
- Runtime-Form-Creation. Automatically creating child forms in a Delphi MDI application with a component array
- CPort Component (Serial port interface component for Delphi)
- SmartImageResizer Plugin, WordPress plugin based on Joe Lencioni’s Smart Image Resizer













