MsgType=GetMarketAuction&Symbol=XLF&AuctionPrice=24.50&AuctionSize=57138&AuctionImbalance=0&AuctionImbalanceMkt=0&AuctionIndNetChange=&ClosingPrice=24.54&BidPrice=24.36&AskPrice=24.37&EC=0&ES=&AuctionImbalanceStatus=&AuctionMktImbalanceStatus=&Delta=
我试图从上面的正则表达式行中提取所需的所有数据。使用VB.net我有前5个东西,但是一旦需要 - / + $号就会被卡住,有时会显示为无字符。
到目前为止这是有效的。
Dim r As New System.Text.RegularExpressions.Regex("Symbol=(?<1>.\w+)\&AuctionPrice=(?<2>.\d.\d+)\&AuctionSize=(?<3>.\d+)\&AuctionImbalance=(?<4>.a*)\&AuctionImbalanceMkt=(?<5>.a*)")
AuctionIndNetChange =(我在这里寻找一些东西来拉取价值)
谢谢!
答案 0 :(得分:0)
您可以使用以下正则表达式:
Symbol=(?<1>[^&]*)&AuctionPrice=(?<2>[^&]*)&AuctionSize=(?<3>[^&]*)&AuctionImbalance=(?<4>[^&]*)&AuctionImbalanceMkt=(?<5>[^&]*)&AuctionIndNetChange=(?<6>[^&]*)
请参阅demo
请注意,您不必在捕获组的名称中使用数字,您可以删除所有?<n>
,您将获得相同的结果。
此外,我使用[^&]*
替换了捕获组中的所有子模式,这意味着 0个或更多字符,而不是&
,因为它们都在查询字符串中
HOWEVER ,您可以使用System.Web.HttpUtility.ParseQueryString
的非正则路由方式(不要忘记添加对System.Web
的引用!):
Dim querystr As String = "MsgType=GetMarketAuction&Symbol=XLF&AuctionPrice=24.50&AuctionSize=57138&AuctionImbalance=0&AuctionImbalanceMkt=0&AuctionIndNetChange=&ClosingPrice=24.54&BidPrice=24.36&AskPrice=24.37&EC=0&ES=&AuctionImbalanceStatus=&AuctionMktImbalanceStatus=&Delta="
Dim qscoll As System.Collections.Specialized.NameValueCollection = HttpUtility.ParseQueryString(querystr)
Console.WriteLine(String.Format("AuctionSize: {0}, Symbol: {1}, AuctionPrice: {2}, AuctionSize: {3}, AuctionImbalance: {4}, AuctionImbalanceMkt: {5}, AuctionIndNetChange: {6}",
qscoll("AuctionSize"), _
qscoll("Symbol"), _
qscoll("AuctionPrice"), _
qscoll("AuctionSize"), _
qscoll("AuctionImbalance"), _
qscoll("AuctionImbalanceMkt"), _
qscoll("AuctionIndNetChange")))
输出:
AuctionSize: 57138, Symbol: XLF, AuctionPrice: 24.50, AuctionSize: 57138, AuctionImbalance: 0, AuctionImbalanceMkt: 0, AuctionIndNetChange: