在Excel VB

时间:2017-04-25 16:52:05

标签: regex excel vb.net replace find

经过多次挫折和阅读文档后,我仍然无法找到运行搜索正则表达式模式的方法,并替换该模式的电子表格中所有匹配项内的字符。

我在单元格中有这个内容:

$test string[0] = "this is a test string."; $another test string[431] = "yet another test string";

这是我的正则表达式模式:

"\$(.*?)\["

输出:

test string, another test string

我需要找到所有的匹配并替换连字符的空格,以便输出为:

test-string, another-test-string

关于如何做到这一点的任何想法?

提前致谢!

2 个答案:

答案 0 :(得分:0)

我猜你可以使用积极的预测,即:

ResultString = Regex.Replace(SubjectString, "(?<=\$.*?) (?=.*?\[)", "-")

答案 1 :(得分:0)

Sub NewNew()
    Dim reg As RegExp
    Set reg = New RegExp

        reg.Pattern = "([a][n][o][t][h][e][r])?([t][e][s][t]) ([s][t][r][i][n][g])"
        reg.Global = True
    Dim str As String
    str = "this is a test string"

    Dim strTwo As String
    strTwo = "yet another test string"

    Dim arr As Variant

    Dim strPrint As String

    Set arr = reg.Execute(str)
    strPrint = arr(0)
    strPrint = Replace(strPrint, " ", "-")
    Debug.Print strPrint

    Set arr = reg.Execute(strTwo)
    strPrint = arr(0)
    strPrint = Replace(strPrint, " ", "-")
    Debug.Print strPrint

End Sub

enter image description here