正则表达式返回整个字符串而不仅仅是捕获组

时间:2018-02-07 20:58:51

标签: c# .net regex

试图获得价值,但正则表达式正在返回BTC","货币":" USD","金额":&#34 ; XXXXX。   服务器响应是

  

{"数据":[{"基础":" BTC""货币":" USD" "量":" 8199.91"},{"基础":" BCH""货币":& #34; USD""量":" 1012.22"},{"基础":" ETH"&#34 ;货币":" USD""量":" 825.94"},{"基础":" LTC& #34;,"货币":" USD","金额":" 150.11"}]}

我怎样才能使用捕获组中的值?

                public async void UpdateValue()
    {
        HttpClient client = new HttpClient();
        {
            try
            {
                HttpResponseMessage response = await client.GetAsync("https://www.coinbase.com/api/v2/prices/USD/spot?");
                string responseBody = await response.Content.ReadAsStringAsync();
                JToken BTC = JObject.Parse(responseBody);
                string btcV = BTC.ToString();
                Regex regex = new Regex("BTC\",\"currency\":\"USD\",\"amount\":\"([^\"]*)");
                Match match = regex.Match(btcV);
                if (match.Success)
                {
                    textBox1.Text = match.Value;
                    Thread.Sleep(1000);
                }
            }
            catch (Exception)
            {
            }
        }
    }

2 个答案:

答案 0 :(得分:2)

如评论中所述,使用JSON.NET库。

直截了当地说:

List<ClassA> objects= JsonConvert.DeserializeObject<List<ClassA>>(json);

查找更多详情https://www.newtonsoft.com/json

答案 1 :(得分:0)

在您的代码中

  • match.Groups[0]是整个匹配项,与您的match.value相同。
  • match.Groups[1]是模式中由()指定的第一个捕获组。这是在小组0中的整个匹配之后。
  • match.Groups[2...]是第二组,依此类推......

你检查了其他组吗?

相关问题