?
Solved

Regular Expression Replace with Many Patterns

Posted on 2005-04-15
3
Medium Priority
?
951 Views
Last Modified: 2008-01-09
Is it possible to use the replace function and define the pattern in the function, so I can have many patterns with only one object, just like in this C++ code: http://www.developer.com/net/cplus/article.php/3495511

If so, how?
0
Comment
Question by:Joakim_
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
snavebelac earned 375 total points
ID: 13791557
Use a regular expression object

Function match(strString, strRegExp)
   Dim objRegExp, colMatches
   Set objRegExp = New RegExp
   objRegExp.Pattern = strRegExp
   objRegExp.IgnoreCase = True
   objRegExp.Global = True
   Set colMatches = objRegExp.Execute(strString)

   'You can then loop through the colMatches collection and prfes all thematches using replace or whatever esle you need

  Set objRegExp = Nothing
End Function

HTH

C
0
 
LVL 2

Author Comment

by:Joakim_
ID: 13793921
That wasn't actually what I was looking for, snavebelac, but when I tried to do what you said, I came up with something else, thanks to you.

By the way, here's what I came up with - a handy function for using RegExp.Replace() function in the same way as Replace(), but this one with a RegExp pattern:

--------------------

Function FormatTextRegExpReplace(strFormatString, strFormatReplacePattern, strFormatReplaceOutput)

      ' Dimension variables.
      Dim objFormatTextOrdinaryRegExp                  ' Holds the "RegExp" object.

      ' Create a regular expression object.
      Set objFormatTextOrdinaryRegExp = New RegExp

      ' Set the regular expression pattern.
      objFormatTextOrdinaryRegExp.Pattern = strFormatReplacePattern

      ' Match all occurrences of the pattern.
      objFormatTextOrdinaryRegExp.Global = True

      ' Set the function value.
      FormatTextRegExpReplace = objFormatTextOrdinaryRegExp.Replace(strFormatString, strFormatReplaceOutput)

      ' Reset server objects.
      Set objFormatTextOrdinaryRegExp = Nothing

End Function

Function FormatText(strString)

      ' Dimension variables.
      Dim strResult                  ' Holds a randomly named variable. (I'll fix this comment later.)

      strResult = FormatTextRegExpReplace(strString, "<", "&lt;")
      strResult = FormatTextRegExpReplace(strResult, ">", "&gt;")
      strResult = FormatTextRegExpReplace(strResult, "T", "&gt;")
      FormatText = strResult

End Function

Response.Write(FormatText("This is a string with some <b>HTML tags</b> in it."))
0
 
LVL 2

Author Comment

by:Joakim_
ID: 13794015
That one replaces "T" with "&gt;", which it shouldn't - I did it just for testing.

Usage of the function: just add as many "strResult = FormatTextRegExpReplace()"s as you want. Put the RegExp pattern in the first quotes, and what it should be replaced with in the second quotes.

In example...

strResult = FormatTextRegExpReplace(strResult, "^;", "&nbsp;&nbsp;&nbsp;")

...Replaces lines starting with ";" indented with three spaces.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
The video provides a quick and easy steps to migrate MBOX file to well known Outlook PST and Office 365. Besides this, it also supports and migrates more than 20 email clients of MBOX which include AppleMail, Opera, Thunderbird and SeaMonkey effortl…
In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question