簡體   English   中英

VBA 正則表達式替換代碼

[英]VBA Regex substitution codes

任何使用 vba 正則表達式替換代碼的經驗? 我已經嘗試了以下方法,它們在 regex101.com 和 regexr.com 上都有效。

$&
\0

不幸的是,它們不適用於我的 VBA 代碼。 有沒有類似的經歷?

示例: https : //regex101.com/r/5Fb0EV/1

VBA代碼:

    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

輸入字符串:

Metodo di pagamento selezionato: 
Mastercard 

預期輸出:

Metodo di pagamento selezionato: 
Mastercard #END

試試下面的代碼:

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

輸入

Metodo di pagamento selezionato:
Mastercard

輸出

Metodo di pagamento selezionato:
Mastercard #END

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM