Majú ťažkosti s vytváraním regexu.
Mám tento text:
"L": 0.01690502, "C": 0.01690502, "V": 33.76590433 "
Potrebujem len číslo po C ": extrahované, to je to, čo v súčasnosti majú.
var regex = new Regex(@"(?<=C\"":)d +.d + (?=s *,\)");
var test = regex.Match(content).ToString();
decimal.TryParse(test, out decimal closingPrice);
odpovede:
3 pre odpoveď č. 1Vybrať číslo po C":
, môžete zachytiť (d+.d+)
v skupine:
Môžete tiež použiť pozitívny lookbehind:
2 pre odpoveď č. 2
Tento kód môžete použiť na načítanie všetkých dvojíc písmen a čísel.
var regex = new Regex("(?<letter>[A-Z])[^:]+:(?<number>[^,"]+)");
var input = "L":0.01690502,"C":0.01690502,"V":33.76590433";
var matches = regex.Matches(input).Cast<Match>().ToArray();
foreach (var match in matches)
Console.WriteLine($"Letter: {match.Groups["letter"].Value}, number: {match.Groups["number"].Value}");
Ak potrebujete iba číslo z písmena "C", môžete použiť tento výraz linq:
var cNumber = matches.FirstOrDefault(m => m.Groups["letter"].Value == "C")?.Groups["number"].Value ?? "";
Vysvetlenie regexu:
(?<letter>[A-Z]) // capture single letter
[^:]+ // skip all chars until ":"
: // colon
(?<number>[^,"]+) // capture all until "," or """
pracovné demonštrácie
1 pre odpoveď č. 3
Opravil to s tým.
var regex = new Regex("(?<=C\":)\d+.\d+(?=\s*,)");
var test = regex.Match(content).ToString();
1 pre odpoveď č. 4
String literal na použitie pre C #:
@"C\"":([.0-9]*),"
Ak chcete filtrovať iba platné čísla:
@"C\"":([0-9]+.[0-9]+),"