How to display all the values ​​that are suitable after the regular expression. The variable contains these values.

[ {"symbol":"ETHBTC","price":"0.03416500"}, {"symbol":"LTCBTC","price":"0.01672200"}, {"symbol":"BNBBTC","price":"0.00349860"}, {"symbol":"NEOBTC","price":"0.00238600"}, {"symbol":"QTUMETH","price":"0.01870700"}, {"symbol":"EOSETH","price":"0.03303200"}, {"symbol":"SNTETH","price":"0.00015567"}, ] 

The code is:

 Regex regex = new Regex(@"(?<=\{).*?(?=})"); Match match = regex.Match(str); System.IO.File.WriteAllText("C:\\Users\\gamen\\source\\repos\\WindowsFormsApp3\\WindowsFormsApp3\\questionAnswers.txt", $"{str}"); 

How to output all values ​​(results) line by line into a text document after regular processing?

  • It is not necessary to parse regulars, it should be parsed as json. - AK
  • json parsit regular - so much pleasure - tym32167
  • What does "line by line" mean? Should ETHBTC be on one line and 0.03416500 on the other? - AK
  • No, the fact that in brackets {"symbol": "ETHBTC", "price": "0.03416500"} on one line. It is spelled out in the regular season, just how to display all the results in a text document. So that later it was convenient to take - Nixens
  • Regarding the regulars and JSON, you have already been told, so I’ll ask one more thing: what is the meaning of interpolation in your example? ( $"{str}" ) - Kir_Antipov

1 answer 1

It is not necessary to parse regulars, it should be parsed as json. Connect Json.Net and parse:

 var request = @"[{""symbol"":""ETHBTC"",""price"":""0.03416500""},{""symbol"":""LTCBTC"",""price"":""0.01672200""},{""symbol"":""BNBBTC"",""price"":""0.00349860""},{""symbol"":""NEOBTC"",""price"":""0.00238600""},{""symbol"":""QTUMETH"",""price"":""0.01870700""},{""symbol"":""EOSETH"",""price"":""0.03303200""},{""symbol"":""SNTETH"",""price"":""0.00015567""}]"; var data = JsonConvert.DeserializeObject<Data[]>(request); var lines = data.Select(x => $"{x.Symbol} - {x.Price}"); File.WriteAllLines(@"C:\temp\123.txt", lines); 

Where Data:

 public class Data { public string Symbol { get; set; } public string Price { get; set; } } 

enter image description here

And in your case - something like this:

 var request = @"[{""symbol"":""ETHBTC"",""price"":""0.03416500""},{""symbol"":""LTCBTC"",""price"":""0.01672200""},{""symbol"":""BNBBTC"",""price"":""0.00349860""},{""symbol"":""NEOBTC"",""price"":""0.00238600""},{""symbol"":""QTUMETH"",""price"":""0.01870700""},{""symbol"":""EOSETH"",""price"":""0.03303200""},{""symbol"":""SNTETH"",""price"":""0.00015567""}]"; var regex = new Regex(@"(?<=\{).*?(?=})"); var match = regex.Matches(request); var lines = match.Cast<Match>().Select(x => x.Value); File.WriteAllLines(@"C:\temp\123.txt", lines); 

enter image description here