在这个问题上抓了好几个小时都无济于事!我需要匹配这个字符串的3个不同部分,有3种不同的模式。所以一个正则表达式模式匹配第一个,等等。
这是字符串:
{
"jsonrpc": "2.0",
"result": [
{
"marketId": "1.123485047",
"isMarketDataDelayed": false,
"status": "OPEN",
"betDelay": 0,
"bspReconciled": false,
"complete": true,
"inplay": false,
"numberOfWinners": 1,
"numberOfRunners": 3,
"numberOfActiveRunners": 3,
"lastMatchTime": "2016-03-18T23:21:14.211Z",
"totalMatched": 47663.09,
"totalAvailable": 140527.57,
"crossMatching": true,
"runnersVoidable": false,
"version": 1259153627,
"runners": [
{
"selectionId": 48224,
"handicap": 0.0,
"status": "ACTIVE",
"lastPriceTraded": 1.57,
"totalMatched": 37408.96,
"ex": {
"availableToBack": [
{
"price": 1.56,
"size": 1344.78
},
{
"price": 1.55,
"size": 642.45
},
{
"price": 1.54,
"size": 1034.4
}
],
"availableToLay": [
{
"price": 1.57,
"size": 303.34
},
{
"price": 1.58,
"size": 2368.34
},
{
"price": 1.59,
"size": 1220.99
}
],
"tradedVolume": []
}
},
{
"selectionId": 1141,
"handicap": 0.0,
"status": "ACTIVE",
"lastPriceTraded": 7.0,
"totalMatched": 5863.62,
"ex": {
"availableToBack": [
{
"price": 7.0,
"size": 286.85
},
{
"price": 6.8,
"size": 552.02
},
{
"price": 6.6,
"size": 25.81
}
],
"availableToLay": [
{
"price": 7.2,
"size": 36.05
},
{
"price": 7.4,
"size": 312.79
},
{
"price": 7.6,
"size": 92.63
}
],
"tradedVolume": []
}
},
{
"selectionId": 58805,
"handicap": 0.0,
"status": "ACTIVE",
"lastPriceTraded": 4.5,
"totalMatched": 4390.5,
"ex": {
"availableToBack": [
{
"price": 4.5,
"size": 209.65
},
{
"price": 4.4,
"size": 692.37
},
{
"price": 4.3,
"size": 429.69
}
],
"availableToLay": [
{
"price": 4.6,
"size": 279.02
},
{
"price": 4.7,
"size": 821.2
},
{
"price": 4.8,
"size": 928.88
}
],
"tradedVolume": []
}
}
]
}
],
"id": 1
}
我加粗了需要提取的3个部分。 (在这种情况下,它们是1.57,7.2和4.6)。这些数字可以是整数,有1或2位小数。
答案 0 :(得分:2)
答案 1 :(得分:0)
更新:刚刚看到“florentbr”的答案,这是一个比我更简洁的答案!
你真的需要RegEx吗?可能有很多方法可以做到这一点,但用逗号分解似乎可以找到合适的数据。打开Excel,将数据转储到单元格“A1”中,然后将此代码转到VBA编辑器中:
Option Explicit
Sub getYourVals()
Dim dataStr() As String
Dim i As Integer
Dim marker As String
marker = "availableToLay"
dataStr = Split(Range("A" & 1).Value, ",")
For i = 1 To UBound(dataStr)
If Mid(dataStr(i), 2, Len(marker)) = marker Then
Debug.Print Mid(dataStr(i), 28, Len(dataStr(i)) - 27)
End If
Range("A" & i + 1).Value = dataStr(i)
Next i
End Sub
答案 2 :(得分:0)
考虑完全解析JSON字符串,将其展平以获取所需数据。 JSON格式可以被认为是字典和集合的集合,VBA维护的两个数据结构。下面使用VBA工具'VBA-JSON库。
设置步骤
VBA 脚本(遍历结果,参赛者和前集合)
Sub JSONStringData()
Dim jsonStr As String
Dim element As Variant, e As Variant, n As Variant, r As Variant
Dim x As Variant, s As Variant, pr As Variant, dat As Variant
Dim i As Integer, num As Integer
jsonStr = "{""jsonrpc"":""2.0"",""result"":" _
& "[{""marketId"":""1.123485047"",""isMarketDataDelayed"":false,""status"":""OPEN""," _
& """betDelay"":0,""bspReconciled"":false,""complete"":true,""inplay"":false," _
& """numberOfWinners"":1,""numberOfRunners"":3,""numberOfActiveRunners"":3,""lastMatchTime"":" _
& """2016-03-18T23:21:14.211Z"",""totalMatched"":47663.09,""totalAvailable"":140527.57,""crossMatching"":true," _
& """runnersVoidable"":false,""version"":1259153627,""runners"":" _
& "[{""selectionId"":48224,""handicap"":0.0,""status"":""ACTIVE"",""lastPriceTraded"":1.57," _
& """totalMatched"":37408.96,""ex"":{""availableToBack"":[{""price"":1.56,""size"":1344.78}," _
& "{""price"":1.55,""size"":642.45},{""price"":1.54,""size"":1034.4}],""availableToLay"":" _
& "[{""price"":1.57,""size"":303.34},{""price"":1.58,""size"":2368.34},{""price"":1.59,""size"":1220.99}]," _
& """tradedVolume"":[]}},{""selectionId"":1141,""handicap"":0.0,""status"":""ACTIVE""," _
& """lastPriceTraded"":7.0,""totalMatched"":5863.62,""ex"":{""availableToBack"":[{""price"":7.0," _
& """size"":286.85},{""price"":6.8,""size"":552.02},{""price"":6.6,""size"":25.81}],""availableToLay"":" _
& "[{""price"":7.2,""size"":36.05},{""price"":7.4,""size"":312.79},{""price"":7.6,""size"":" _
& "92.63}],""tradedVolume"":[]}},{""selectionId"":58805,""handicap"":0.0,""status"":" _
& """ACTIVE"",""lastPriceTraded"":4.5,""totalMatched"":4390.5,""ex"":{""availableToBack"":" _
& "[{""price"":4.5,""size"":209.65},{""price"":4.4,""size"":692.37},{""price"":4.3,""size"":429.69}]" _
& ",""availableToLay"":[{""price"":4.6,""size"":279.02},{""price"":4.7,""size"":821.2}," _
& "{""price"":4.8,""size"":928.88}],""tradedVolume"":[]}}]}],""id"":1}"
' PARSE FILE STRING
Dim p As Object
Set p = ParseJson(jsonStr)
i = 5
For Each element In p
' RESULT COLLECTION OF DICTIONARIES (N = 17)
If element = "result" Then
For Each e In p(element)
For Each n In e
' RUNNERS COLLECTION OF DICTIONARIES (N = 6)
If n = "runners" Then
For Each r In e(n)
For Each x In r
' EX DICTIONARY OF COLLECTIONS (N = 9, 3 PER 3 RUNNERS)
If x = "ex" Then
For Each s In r(x)
num = 1
For Each pr In r(x)(s)
For Each dat In pr
Sheets(1).Range("A" & i) = r("selectionId")
Sheets(1).Range("B" & i) = r("handicap")
Sheets(1).Range("C" & i) = r("status")
Sheets(1).Range("D" & i) = r("lastPriceTraded")
Sheets(1).Range("E" & i) = r("totalMatched")
Sheets(1).Range("F" & i) = s
Sheets(1).Range("G" & i) = num
Sheets(1).Range("H" & i) = dat
Sheets(1).Range("I" & i) = pr(dat)
i = i + 1
Next dat
num = num + 1
Next pr
Next s
End If
Next x
Next r
End If
Next n
Next e
End If
Next element
End Sub
结果 (其中1.57,7.2和4.6是三个参赛者中每个参赛者的可用的第一个价格)
48224 0 ACTIVE 1.57 37408.96 availableToBack 1 price 1.56
48224 0 ACTIVE 1.57 37408.96 availableToBack 1 size 1344.78
48224 0 ACTIVE 1.57 37408.96 availableToBack 2 price 1.55
48224 0 ACTIVE 1.57 37408.96 availableToBack 2 size 642.45
48224 0 ACTIVE 1.57 37408.96 availableToBack 3 price 1.54
48224 0 ACTIVE 1.57 37408.96 availableToBack 3 size 1034.4
48224 0 ACTIVE 1.57 37408.96 availableToLay 1 price 1.57
48224 0 ACTIVE 1.57 37408.96 availableToLay 1 size 303.34
48224 0 ACTIVE 1.57 37408.96 availableToLay 2 price 1.58
48224 0 ACTIVE 1.57 37408.96 availableToLay 2 size 2368.34
48224 0 ACTIVE 1.57 37408.96 availableToLay 3 price 1.59
48224 0 ACTIVE 1.57 37408.96 availableToLay 3 size 1220.99
1141 0 ACTIVE 7 5863.62 availableToBack 1 price 7
1141 0 ACTIVE 7 5863.62 availableToBack 1 size 286.85
1141 0 ACTIVE 7 5863.62 availableToBack 2 price 6.8
1141 0 ACTIVE 7 5863.62 availableToBack 2 size 552.02
1141 0 ACTIVE 7 5863.62 availableToBack 3 price 6.6
1141 0 ACTIVE 7 5863.62 availableToBack 3 size 25.81
1141 0 ACTIVE 7 5863.62 availableToLay 1 price 7.2
1141 0 ACTIVE 7 5863.62 availableToLay 1 size 36.05
1141 0 ACTIVE 7 5863.62 availableToLay 2 price 7.4
1141 0 ACTIVE 7 5863.62 availableToLay 2 size 312.79
1141 0 ACTIVE 7 5863.62 availableToLay 3 price 7.6
1141 0 ACTIVE 7 5863.62 availableToLay 3 size 92.63
58805 0 ACTIVE 4.5 4390.5 availableToBack 1 price 4.5
58805 0 ACTIVE 4.5 4390.5 availableToBack 1 size 209.65
58805 0 ACTIVE 4.5 4390.5 availableToBack 2 price 4.4
58805 0 ACTIVE 4.5 4390.5 availableToBack 2 size 692.37
58805 0 ACTIVE 4.5 4390.5 availableToBack 3 price 4.3
58805 0 ACTIVE 4.5 4390.5 availableToBack 3 size 429.69
58805 0 ACTIVE 4.5 4390.5 availableToLay 1 price 4.6
58805 0 ACTIVE 4.5 4390.5 availableToLay 1 size 279.02
58805 0 ACTIVE 4.5 4390.5 availableToLay 2 price 4.7
58805 0 ACTIVE 4.5 4390.5 availableToLay 2 size 821.2
58805 0 ACTIVE 4.5 4390.5 availableToLay 3 price 4.8
58805 0 ACTIVE 4.5 4390.5 availableToLay 3 size 928.88