dcsimg
Results 1 to 5 of 5

Thread: [RESOLVED] Use Regular Expressions to replace strings

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    1,617

    Resolved [RESOLVED] Use Regular Expressions to replace strings

    I need to use RegExp to complete some strings replacement operations, for example: “Hello” --> “Hello”

    My code is as follows:

    Code:
    Private Sub Command1_Click()
        Dim RE As New VBScript_RegExp_55.RegExp
        Dim M As VBScript_RegExp_55.Match
        Dim sText As String
        
        sText = "Hello"          '--- Hello ---
        
        RE.Pattern = "&#([0-9]+);"
        RE.Global = True
        
        'For Each M In RE.Execute(sText)
        '    MsgBox M.Value
        'Next
        
        MsgBox RE.Replace(sText, "??????")
    
    End Sub
    I don't know how to do this with RegExp.Replace.

  2. #2
    Frenzied Member
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    1,360

    Re: Use Regular Expressions to replace strings

    On way you can do it is to turn of the Global search and roll your own replace loop. Something like this:

    Code:
    Public Sub Test()
       Dim RE As VBScript_RegExp_55.RegExp
       Dim M As VBScript_RegExp_55.Match
       Dim MC As VBScript_RegExp_55.MatchCollection
       Dim sText As String
    
       Set RE = CreateObject("VBScript.RegExp")
    
       sText = "Hello"          '--- Hello ---
    
       RE.Pattern = "&#([0-9]+);"
    
       Do
          Set MC = RE.Execute(sText)
          For Each M In MC
             If M.SubMatches.Count Then
                sText = Replace$(sText, M.Value, ChrW$(M.SubMatches(0))) ' Replace the match with the CHRW$ the numeric submatch of the match
             End If
          Next M
       Loop While MC.Count  ' Repeat this loop for each match
    
       Debug.Print sText
    End Sub
    Not sure if there's a way to substitute a match for the ChrW$() of that match in a Global search (where the regex engine handles the replacements for you).

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    1,617

    Re: Use Regular Expressions to replace strings

    Hi jpbro, I tested your code and it worked very well. thank you very much.

    I'm thinking about a problem, I saw a piece of code on PsCode (VBScript Editor + Regular Expression Editor), but it didn't run successfully:

    Class: CRX_ReplaceArrays
    Code:
    Public Function Replacer(Match As String, SubMatch1 As String, SubMatch2 As String, Index As Long, FullText As String) As String
    Dim idx As Integer, sArrayVar As String, sTemp As String
    Dim iNumItems As Long, vTempArray As Variant
    
    
    sTemp = Trim$(SubMatch1) & " = Array(" & SubMatch2 & ")" & vbCrLf
    
    'vTempArray = Split(SubMatch2, ",")
    'iNumItems = UBound(vTempArray) - LBound(vTempArray) + 1
    'For idx = LBound(vTempArray) To UBound(vTempArray)
    '    sTemp = sTemp & sArrayVar & "(" & Format$(idx) & ")=" & vTempArray(idx) & vbCrLf
    'Next
    'sTemp = "Dim " & sArrayVar & "(" & Format$(iNumItems - 1) & ")" & vbCrLf & sTemp & vbCrLf
    
    Replacer = sTemp
    
    End Function
    Code:
    Public Function RX_ExpandArrays(ByVal Text As String) As String
    
    Dim objRegExp As RegExp
    Set objRegExp = New RegExp
    
    objRegExp.IgnoreCase = True
    objRegExp.Global = True
    objRegExp.MultiLine = True
    
    objRegExp.Pattern = "^([\w ]*?)= *?\[(.*?)\].*?\r\n"
    
    
    RX_ExpandArrays = objRegExp.Replace(Text, New CRX_ReplaceArrays)    '--- The code here cannot be executed
    
    Set objRegExp = Nothing
    
    End Function
    Last edited by dreammanor; May 25th, 2019 at 10:35 PM.

  4. #4
    Frenzied Member
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    1,360

    Re: Use Regular Expressions to replace strings

    Did you set the "Default" procedure ID for the Replacer function?

    Name:  ProcId.png
Views: 67
Size:  31.5 KB

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    1,617

    Re: Use Regular Expressions to replace strings

    Quote Originally Posted by jpbro View Post
    Did you set the "Default" procedure ID for the Replacer function?

    Name:  ProcId.png
Views: 67
Size:  31.5 KB
    Hi jpbro, after setting the "Default" procedure ID for the Replacer function, the code is now working, thank you very much!

    Also, can we pass a callback function to RegExp.Replace? E.g: RE.Replace(sText, AddressOf Func_Replacer)

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width