在具有特定值的字符串中查找“~XX~”

Find '~XX~' within a string with specific values

我有经典的 ASP 用 VBScript 编写的。我从 SQL 服务器中提取了一条记录,数据是一个字符串。在这个字符串中,我需要找到包含在 ~12345~ 中的文本,我需要用非常具体的文本替换。例如 1 将替换为 M2 将替换为 A。然后我需要在网页上显示它。我们不知道 ~.

将包含多少项

示例数据:

Group Pref: (To be paid through WIT)
~2.5~ % Quarterly Rebate - Standard Commercial Water Heaters

网页显示时间:

Group Pref: (To be paid through WIT)
~A.H~ % Quarterly Rebate - Standard Commercial Water Heaters

我尝试了以下方法,但是有两种情况,维护起来是不现实的。我确实替换了文本并正确显示。

dim strSearchThis
strSearchThis =(rsResults("PREF"))
set re = New RegExp

with re
    .global = true
    .pattern = "~[^>]*~"
    strSearchThis = .replace(strSearchThis, "X")
end with

我也在尝试这段代码,我可以找到每个~~之间包含的文本,但是当显示它时~~之间的信息没有改变:

dim strSearchThis
strSearchThis =(rsResults("PREF"))

Set FolioPrefData = New RegExp
FolioPrefData.Pattern = "~[^>]*~"
FolioPrefData.Global = True 
FolioPrefData.IgnoreCase = True

'will contain all found instances of ~ ~'
set colmatches = FolioPrefData.Execute(strSearchThis)

Dim itemLength, found

For Each objMatch in colMatches
    Select Case found
        Case "~"
        'ignore - doing nothing'
         Case "1"
         found = replace(strSearchThis, "M")

    End Select
Next
response.write(strSearchThis)

您可以在不使用正则表达式的情况下做到这一点,只需检查各个字符并编写一个函数来处理您拥有的不同情况。以下函数查找分隔文本并遍历所有字符,调用进一步定义的 ReplaceCharacter 函数:

Function FixString(p_sSearchString) As String
    Dim iStartIndex
    Dim iEndIndex
    Dim iIndex
    Dim sReplaceString
    Dim sReturnString

    sReturnString = p_sSearchString

    ' Locate start ~
    iStartIndex = InStr(sReturnString, "~")

    Do While iStartIndex > 0

        ' Look for end ~
        iEndIndex = InStr(iStartIndex + 1, sReturnString, "~")

        If iEndIndex > 0 Then

            sReplaceString = ""

            ' Loop htrough all charatcers
            For iIndex = iStartIndex + 1 To iEndIndex - 1
                sReplaceString = sReplaceString & ReplaceCharacter(Mid(sReturnString, iIndex, 1))
            Next

            ' Replace string
            sReturnString = Left(sReturnString, iStartIndex) & sReplaceString & Mid(sReturnString, iEndIndex)

            ' Locate next ~
            iStartIndex = InStr(iEndIndex + 1, sReturnString, "~")

        Else
            ' End couldn't be found, exit
            Exit Do
        End If

    Loop

    FixString = sReturnString

End Function

这是您将输入可能具有的不同字符替换的函数:

Function ReplaceCharacter(p_sCharacter) As String
    Select Case p_sCharacter
        Case "1"
            ReplaceCharacter = "M"
        Case "2"
            ReplaceCharacter = "A"
        Case Else
            ReplaceCharacter = p_sCharacter
    End Select

End Function

您可以在现有代码中使用它:

response.write(FixString(strSearchThis))

您还可以使用 SplitJoin 方法...

Const SEPARATOR = "~"

Dim deconstructString, myOutputString
Dim arrayPointer

deconstructString = Split(myInputString, SEPARATOR)

For arrayPointer = 0 To UBound(deconstructString)
    If IsNumeric(deconstructString(arrayPointer)) Then
        'Do whatever you need to with your value...
    End If
Next 'arrayPointer

myOutputString = Join(deconstructString, "")

很明显,这确实依赖于将字符串分开并重新加入,因此在字符串可变性问题上有一个巧妙的开销。