Results 1 to 6 of 6
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Regular Expressions - replace multiple (WordXP)

    I'm new to Regular Expressions in VBA, and have managed to accomplish what i want to do (below), but feel that there has to be a better way. My example is a hastily-written in-line thing that gets my particular job done. I figure that i could use a loop, repeatedly calling regexp.Replace until there were no substrings greater than one digit ([0-9]). Is there a way to use RegExp's abilities without resorting to a VBA loop?

    The input string is a legal-like numbering scheme. I want to reduce the digits streams to a generic pattern. The test code below shows that "125345.6.7" will be reduced to <digit><period><digit><period><digit>, a.k.a. a level-3 type of number.
    <pre>Public Function strRegExpReplace(strIn, patrn, strReplace) As String
    Dim regex As regExp
    Set regex = New regExp
    regex.Pattern = patrn
    regex.IgnoreCase = True
    regex.Global = True
    strRegExpReplace = regex.Replace(strIn, strReplace)
    End Function
    Sub TESTRegExpReplace()
    Dim strResult As String
    strResult = "125345.6.7"
    strResult = strRegExpReplace(strResult, "[0-9][0-9][0-9][0-9]", "9")
    strResult = strRegExpReplace(strResult, "[0-9][0-9]", "9")
    strResult = strRegExpReplace(strResult, "[0-9][0-9]", "9")
    MsgBox strRegExpReplace(strResult, "[0-9]", "^#")
    End Sub</pre>


    Credit to Andrew for the basis of my example.

  2. #2
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Regular Expressions - replace multiple (WordXP

    Hi Chris,

    Let's talk more about your desired result here. You want to match any number of the form 1234.5.6 (i.e., one or more numbers followed by a period followed by one or more numbers followed by a period, followed by one or more number) and replace it with a generic "^#.^#.^# ? Is that correct? will there always be three sets of digits with periods in between? Can you give me a list of 5 possible input strings, and what your desired output strings would be?

    Regular Experssions are very powerful; I'd be surprised if we can't find a way to do what you're looking for without resorting to a loop.

  3. #3
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Regular Expressions - replace multiple (WordXP

    If I'm understanding your query correctly, this should get you closer:
    <pre>Function ReplaceLongNumbersWithPattern(str As String) As String
    Dim regex As RegExp
    Dim strReplacementPattern As String
    Dim strRegexPattern As String

    strReplacementPattern = "^#.^#.^#"
    strRegexPattern = "bd+.d+.d+b"

    Set regex = New RegExp
    With regex
    .Pattern = strRegexPattern
    .IgnoreCase = True
    .Global = True
    End With
    ReplaceLongNumbersWithPattern = regex.Replace(str, "^#.^#.^#")
    End Function
    '--------
    Sub TestLongNumberReplacement()
    Dim str As String
    str = "The quick brown fox jumps 124.4.5 over" & vbCr & "884.4.221" & vbCr & "the lazy dog"
    MsgBox ReplaceLongNumbersWithPattern(str)
    End Sub
    </pre>


  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Regular Expressions - replace multiple (WordXP

    Chris, there is some documentation on the Pattern property at Microsoft.com. I linked to it in these posts: <post#=88215>post 88215</post#> and <post#=273098>post 273098</post#> but I didn't go back to verify the links today...

  5. #5
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Regular Expressions - replace multiple (WordXP

    Andrew, thanks for the two replies.

    d+ was what I lacked in my embryonic knowledge, thanks. You can see that I'd got as far as the [0-9] concept, but hadn't plucked up courage to use the "d" shorthand (although I'd seen "digit" in my travels).

    I'm going to re-read all the references I've accumulated on regular expressions and try to find a use for each aspect; it's my way of learning. I'm struck by the simliarity to the pattern-matching of Poole&Waite's "Stage2" macro compiler. I'd contemplated implementing a version as a VBA library before I stumbled on Regular Expressions.

  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Regular Expressions - replace multiple (WordXP

    Jefferson, thanks for the response. Both links are still valid! More grist for the mill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •