有以下字符串,S="<%AUTHENTICATED(1)%><%USERNAME(管理员)%><%USERTOKEN(7D7327DD-56EC-4C8A-AE71-2240A3AE2254)%>",
写一个函数,
我传 GetFieldValue("AUTHENTICATED",S) 侧返回 1,传 GetFieldValue("USERNAME",S) 得到 管理员,传  GetFieldValue("USERTOKEN",S) ,得到 7D7327DD-56EC-4C8A-AE71-2240A3AE2254,以此类推,如何得到以下值,我要得到AUTHENTICATED=1,Public Function GetFieldValue(FieldName As String, SourTxt as String) As String
     
   
End Function请高手指教!

解决方案 »

  1.   

    伪代码,大概写的,只是思路
    Public Function GetFieldValue(FieldName As String, SourTxt as String) As String
         start = instr(FieldName, FieldName & "(") + len(FieldName) + 1
         end = instr(FieldName, ")", start) - 1
         GetFieldValue = Mid(SourTxt, start, end - start)
    End Function
      

  2.   

    Public Function GetFieldValue(FieldName As String, SourTxt As String) As String
       Dim i&, j&
       
       i = InStr(SourTxt, FieldName)
       If (i > 0) Then
          i = InStr(i, SourTxt, "(") + 1
          j = InStr(i, SourTxt, ")")
          GetFieldValue = Mid$(SourTxt, i, j - i)
       Else
          GetFieldValue = ""
       End If
    End Function
      

  3.   

    Private Const S = "<%AUTHENTICATED(1)%><%USERNAME(管理员)%><%USERTOKEN(7D7327DD-56EC-4C8A-AE71-2240A3AE2254)%>"Private Sub Command1_Click()
      MsgBox GetData("USERTOKEN", S)
    End Sub
    Private Function GetData(ByVal Title As String, ByVal inFind As String) As String
       Dim mhs As Object
       Dim re As Object
       Dim mh As Object
       
       GetData = "N/A"
       
       Set re = CreateObject("vbscript.regExp")
       re.Global = True
       re.IgnoreCase = True
       re.Pattern = "<%" & Title & "\(([^\)]*)\)%>"
       Set mhs = re.Execute(inFind)
       If mhs.Count > 0 Then
          Set mh = mhs(0)
          GetData = mh.SubMatches(0)
       End If
    End Function
      

  4.   

    Public Function GetFieldValue(FieldName As String, SourTxt As String) As String
    Dim p As Integer
    Dim strSubString As String
    Dim strArr() As Stringp = InStr(SourTxt, "%" & FieldName)
    strSubString = Mid(SourTxt, p + Len(FieldName) + 2)
    strArr = Split(strSubString, ")%")GetFieldValue = IIf(p, strArr(0), "")
    End Function
      

  5.   

    更严谨一点:
    Public Function GetFieldValue(FieldName As String, SourTxt As String) As String
    Dim p As Integer
    Dim strArr() As String    p = InStr(SourTxt, "%" & FieldName & "(")
        strArr = Split(Mid(SourTxt, p + Len(FieldName) + 2), ")%")
        
        GetFieldValue = IIf(p, strArr(0), "")
    End Function