Replacing and filtering text with regular expression using Delphi, Visual Basic and ASP

Posted by Daniel - 3,602 Views

regular expression,replacing text with regex,filtering text with regex,regex exampleA 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.

function TForm1.PerformRegExReplacing(strInput, strRegExPattern, strReplacement: string): string;
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.

Function PerformRegExReplacement(strInput, strPattern, strReplacement) As String
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.

Imports System.Text.RegularExpressions

And the function example is below

Private Function PerformRegexFiltering(ByVal strInput, ByVal strPattern) As String
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 :)

__________________________

Related posts brought to you by Yet Another Related Posts Plugin.

share this article

Digg del.icio.us Netscape StumbleUpon Yahoo! MyWeb reddit Furl Magnolia Newsvine Technorati SlashDot Blinklist Simpy Google
This post as PDFPosted in: Programming - January 2008

6 Responses to “Replacing and filtering text with regular expression using Delphi, Visual Basic and ASP”

  1. littleprincess Says:

    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..

  2. Daniel Says:

    @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 *.EXE
    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

  3. littleprincess Says:

    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..

  4. Daniel Says:

    @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

  5. Keith B Says:

    Daniel,

    Nice piece of visual basic code. Thanks for sharing this.

    Keith

  6. Daniel Says:

    @Keith: Right back at you Keith :)
    _______________

    Daniel



Leave a Reply


Options for your comment:





Get my Full Feed Here or you can subscribe to one of my category based feeds below:
Coffee Break

Latest Blog Entries

Categories

Neighbours and Friends

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.