在VBScript中按照正则表达式拆分字符串

3

我想按照正则表达式将字符串分割成数组,类似于PHP中的preg_split或VBScript中的Split函数,但是使用正则表达式作为定界符。

使用VBScript Regexp对象,我可以执行一个正则表达式,但它返回匹配项(所以我得到了我的分隔符的集合...这不是我想要的)

有没有办法实现这个功能?

谢谢

4个回答

5
如果您可以保留一个特殊的分隔符字符串,即一个您可以选择的字符串,它永远不会成为真实输入字符串的一部分(例如"#@#"),那么您可以使用正则表达式替换将您的模式的所有匹配项替换为"#@#",然后在"#@#"上进行拆分。
另一种可能性是使用捕获组。如果您的分隔符正则表达式是,比如,\d+,那么您搜索(.*?)\d+,然后提取每个匹配中组捕获的内容(在rubular.com上查看前和)。

很好。 对于样本字符串'james007bond123hello42world',正则表达式应该是什么,以便在从字母(d+)到数字(D+)的每个更改处进行拆分? 预期结果:$1='james' $2='007' $3='bond' $4='123' $6='hello' $7='42' 'world' 这可能吗? - snahl
这是一种非常有前途的方法,但它忽略了特殊字符,如“_”或“.”: https://rubular.com/r/Q92iW17MALZPD7 - snahl

0

我认为你可以通过使用Execute来匹配所需的分隔符字符串,但是将所有前面的字符(在上一个匹配之后)捕获为一组。以下是一些可以实现你想要的功能的代码。

'// Function splits a string on matches
'// against a given string
Function SplitText(strInput,sFind)
    Dim ArrOut()


    '// Don't do anything if no string to be found
    If len(sFind) = 0 then
        redim ArrOut(0)
        ArrOut(0) = strInput
        SplitText = ArrOut
        Exit Function
    end If

    '// Define regexp
    Dim re
    Set re = New RegExp 

    '// Pattern to be found - i.e. the given
    '// match or the end of the string, preceded
    '// by any number of characters
    re.Pattern="(.*?)(?:" & sFind & "|$)" 
    re.IgnoreCase = True 
    re.Global = True

    '// find all the matches >> match collection
    Dim oMatches: Set oMatches = re.Execute( strInput )

    '// Prepare to process
    Dim oMatch
    Dim ix
    Dim iMax

    '// Initialize the output array
    iMax = oMatches.Count - 1
    redim arrOut( iMax)

    '// Process each match 
    For ix = 0 to iMax

        '// get the match
        Set oMatch = oMatches(ix)


        '// Get the captured string that precedes the match
        arrOut( ix ) = oMatch.SubMatches(0)

    Next

    Set re = nothing

    '// Check if the last entry was empty - this
    '// removes one entry if the string ended on a match
    if arrOut(iMax) = "" then Redim Preserve ArrOut(iMax-1)

    '// Return the processed output
    SplitText = arrOut

End Function

我看到这实际上是 @polygenelubricants 答案中第二个建议的重复,该建议已经被接受。 - JohnRC

0
您可以始终使用匹配的数组作为输入传递给“split”函数。您可以使用第一个匹配项来分割原始字符串 - 字符串的第一部分是第一次分割,然后分割字符串的其余部分(减去第一部分和第一匹配项)......直到完成。

1
如果我想将一个多行字符串拆分成单独的变量,使用匹配换行符\n的数组可能行不通。看起来它只会寻找字符串\n而不是寻找换行符,对吗? - Michael Innes

0

我写这个是为了自己使用。也许正是你所需要的。

Function RegSplit(szPattern, szStr)
Dim oAl, oRe, oMatches
Set oRe = New RegExp
oRe.Pattern = "^(.*)(" & szPattern & ")(.*)$"
oRe.IgnoreCase = True
oRe.Global = True
Set oAl = CreateObject("System.Collections.ArrayList")

Do
    Set oMatches = oRe.Execute(szStr)
    If oMatches.Count > 0 Then
        oAl.Add oMatches(0).SubMatches(2)
        szStr = oMatches(0).SubMatches(0)
    Else
        oAl.Add szStr
        Exit Do
    End If  
Loop
oAl.Reverse
RegSplit = oAl.ToArray
End Function
'**************************************************************
Dim A
A = RegSplit("[,|;|#]", "bob,;joe;tony#bill")
WScript.Echo Join(A, vbCrLf)

Returns:
bob

joe
tony
bill

这似乎无法正确处理带有多个匹配项的字符串模式链接\s+ - NetMage

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接