Regular Expression Replace with Many Patterns

Posted on 2005-04-15
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:

If so, how?
Question by:Joakim_
    LVL 6

    Accepted Solution

    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


    LVL 2

    Author Comment

    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."))
    LVL 2

    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Title # Comments Views Activity
    Dynamic? Dropdown Box 29 66
    SELECT TOP 5 COUNT(*) need a variable for it 9 33
    Leverage browser caching 2 41
    Time/Date Query 11 28
    I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
    This demonstration started out as a follow up to some recently posted questions on the subject of logging in: and…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now