简体   繁体   中英

VBA Regex substitution codes

any experience with vba regex substition codes? I've tried the followings, which are working both on regex101.com and on regexr.com.

$&
\0

They are unfortunately not working in my VBA code. Any similar experience?

Example: https://regex101.com/r/5Fb0EV/1

VBA code:

    Dim MsgTxt As String
    ...

    strPattern = "(Metodo di pagamento).*\r\x07?.*"  
    With regEx
        .Global = True
        .MultiLine = True
        .IgnoreCase = True
        .Pattern = strPattern
        MsgTxt = regEx.Replace(MsgTxt, "\0#END")
    End With

Input string:

Metodo di pagamento selezionato: 
Mastercard 

Expected ouput:

Metodo di pagamento selezionato: 
Mastercard #END

Try the below code:

Sub test()

    Dim MsgTxt As String

    MsgTxt = Chr(7) & "Metodo di pagamento selezionato:" & vbCr & Chr(7) & "Mastercard "
    With New RegExp
        .Global = True
        .MultiLine = True
        .IgnoreCase = True
        .Pattern = "(Metodo di pagamento.*\r\x07?.*)"
        MsgTxt = .Replace(MsgTxt, "$1#END")
    End With
    Debug.Print MsgTxt

End Sub

Input

Metodo di pagamento selezionato:
Mastercard

Output

Metodo di pagamento selezionato:
Mastercard #END

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM