0

我似乎无法弄清楚如何使用 VB.net 从我的字符串中获取值

如果我的文本框中有一个字符串,上面写着:

WWW-Authenticate: Digest realm="MyServer",qop="auth",algorithm="MD5",maxbuf=1000,nonce="3b010c090c0a0000c0a80157c7007f03c5",opaque="4e6573732041636365737320436f6e74"

如何获取字符串中 = 之后的每个值。

我试过使用

Dim s = "WWW-Authenticate: Digest realm='MyServer',qop='auth',algorithm='MD5',maxbuf=1000,nonce='3b010c090c0a0000c0a80157c7007f03c5',opaque='4e6573732041636365737320436f6e74'"
                        Dim pattern = "="
                        Dim matches = Regex.Matches(s, pattern)
                        Dim values = matches.OfType(Of Match).Select(Function(m) m.Value)

                        For Each v In values
                            MsgBox(v)
                        Next

但它只返回消息框中的 =。

我希望能够得到 = 符号之后的部分。

有谁能帮忙吗?

我尝试使用以下内容,但它仍然在字符串中包含 realm= qop= etc..。(但将其包含在下一个项目的末尾。

 Dim s = "WWW-Authenticate: Digest realm='Ness Access Control',qop='auth',algorithm='MD5',maxbuf=1000,nonce='3b010c090c0a0000c0a80157c7007f03c5',opaque='4e6573732041636365737320436f6e74'"
                        Dim result_array As Array = Split(s, "=", 6)

                        For Each v In result_array
                            MsgBox(v)
                        Next
4

3 回答 3

2

常用表达!

Imports System.Text.RegularExpressions
Module Module1
    Sub Main()
        Dim s As String = "WWW-Authenticate: Digest realm='MyServer',qop='auth',algorithm='MD5',maxbuf=1000,nonce='3b010c090c0a0000c0a80157c7007f03c5',opaque='4e6573732041636365737320436f6e74'"

        'Regular Expression, matches word before equals, and word after equals
        Dim r As New Regex("(\w+)\='([^']+)'")

        'All the matches!
        Dim matches As MatchCollection = r.Matches(s)
        For Each m As Match In matches
            'm.Groups(1) = realm, qop, algorithm...
            'm.Groups(2) = MyServer, auth, MD5...
            Console.WriteLine(m.Groups(2))
        Next
        Console.ReadLine()
    End Sub
End Module

如果你想要一个漂亮的键值字典中的所有内容:

    Dim dict As New Dictionary(Of String, String)
    For Each m As Match In matches
        'm.Groups(1) = realm, qop, algorithm...
        'm.Groups(2) = MyServer, auth, MD5...
        dict(m.Groups(1).ToString()) = dict(m.Groups(2).ToString())
    Next
于 2013-02-06T23:49:00.187 回答
0

您正在寻找split()函数。

Dim logArray() As String
logArray = Split(s, "=")
For count = 0 To logArr.Length - 1
    MsgBox(logArray(count))
Next
于 2013-02-06T23:33:26.400 回答
0

特定字符串扩展的案例。如何使用键和值转换字典中的特定格式化字符串

Public Module StringModuleExtensions
    <Extension()>
    Public Function ToStringDictionary(ByVal str as String, _
                                      ByVal OuterSeparator as Char, _
                                      ByVal  NameValueSeparator as Char) _
                                      As Dictionary(of String, String)
        Dim dicText = New Dictionary(Of String, String)()
        if Not String.IsNullOrEmpty(str) then
            Dim arrStrings() = str.TrimEnd(OuterSeparator).Split(OuterSeparator)
            For Each s in arrStrings
                Dim posSep = s.IndexOf(NameValueSeparator)
                Dim name = s.Substring(0, posSep)
                Dim value = s.Substring(posSep + 1)
                dicText.Add(name, value)
            Next
        End If
        return dicText
    End Function
End Module

打电话给

Dim test = "WWW-Authenticate: Digest realm=""MyServer"",qop=""auth"",algorithm=""MD5"", maxbuf=1000,nonce=""3b010c090c0a0000c0a80157c7007f03c5"",opaque=""4e6573732041636365737320436f6e74"""
Dim dict = test.ToStringDictionary(","c, "="c)
For Each s in dict.Keys
    Console.WriteLine(dict(s))
Next

(可能您之前需要删除 WWW-Authenticate 行。

于 2013-02-07T00:12:23.063 回答