public void GroupCollectionTest() { var regex = VirtualRoot.GetRegex("sss"); var match = regex.Match("sdsd"); Assert.IsTrue(string.IsNullOrEmpty(match.Groups["hello"].Value)); }
public void RegexReplaceTest() { Regex regex = VirtualRoot.GetRegex(@"t="); string text = @"11:55:42:201 384 ETH: GPU0 t=88 fan=77, GPU1 t=66 fan=99"; text = regex.Replace(text, "温度="); regex = VirtualRoot.GetRegex(@"fan="); text = regex.Replace(text, "风扇="); Console.WriteLine(text); }
public void RegexTest3() { string line = @"04:33:17:677 1b04 buf: {""result"":true,""id"":17}"; Regex regex = VirtualRoot.GetRegex(@"{""result"":true,""id"":1(?<gpu>\d+)}"); var match = regex.Match(line); Assert.IsTrue(match.Success); string gpuText = match.Groups[NTKeyword.GpuIndexGroupName].Value; Assert.AreEqual("7", gpuText); Assert.IsTrue(string.IsNullOrEmpty(match.Groups["aaa"].Value)); }
private static List <IncomeItem> PickIncomeItems(string html) { try { List <IncomeItem> results = new List <IncomeItem>(); if (string.IsNullOrEmpty(html)) { return(results); } string patternFileFullName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "pattern.txt"); if (!File.Exists(patternFileFullName)) { return(results); } string pattern = File.ReadAllText(patternFileFullName); if (string.IsNullOrEmpty(pattern)) { return(results); } List <int> indexList = new List <int>(); const string splitText = "<tr class=\"row-common"; int index = html.IndexOf(splitText); while (index != -1) { indexList.Add(index); index = html.IndexOf(splitText, index + splitText.Length); } Regex regex = VirtualRoot.GetRegex(pattern); for (int i = 0; i < indexList.Count; i++) { IncomeItem incomeItem; if (i + 1 < indexList.Count) { incomeItem = PickIncomeItem(regex, html.Substring(indexList[i], indexList[i + 1] - indexList[i])); } else { string content = html.Substring(indexList[i]); incomeItem = PickIncomeItem(regex, content.Substring(0, content.IndexOf("<span class=\"info-title\">币价</span>"))); } if (incomeItem != null) { results.Add(incomeItem); } } return(results); } catch (Exception e) { Logger.ErrorDebugLine(e); return(new List <IncomeItem>()); } }
public void RegexTest1() { string line = "11.1 h/s | 12.2 h/s | 13.3 h/s"; Regex regex = VirtualRoot.GetRegex(@"(?<gpuSpeed>\d+\.?\d*) (?<gpuSpeedUnit>.+?/s)(?: \|)?"); MatchCollection matches = regex.Matches(line); for (int gpuId = 0; gpuId < matches.Count; gpuId++) { Match match = matches[gpuId]; string gpuSpeedUnit = match.Groups["gpuSpeedUnit"].Value; double.TryParse(match.Groups["gpuSpeed"].Value, out double gpuSpeed); Console.WriteLine($"GPU{gpuId.ToString()} {gpuSpeed.ToString()} {gpuSpeedUnit}"); } }
public void RegexTest() { Regex regex = VirtualRoot.GetRegex(@"GPU(?<gpu>\d+) (?<gpuSpeed>[\d\.]+) (?<gpuSpeedUnit>.+?/s)"); string text = @"11:55:42:201 384 ETH: GPU0 14.015 Mh/s, GPU1 21.048 Mh/s"; MatchCollection matches = regex.Matches(text); foreach (Match match in matches) { Console.WriteLine(match.Groups["gpu"]); Console.WriteLine(match.Groups["gpuSpeed"]); Console.WriteLine(match.Groups["gpuSpeedUnit"]); Console.WriteLine("notexit=" + match.Groups["notexit"]); } }
public void RegexTest4() { string line = @"17:18:33:747 a88 buf: {""result"":true,""id"":12}"; string pattern = @"buf:.+(""result"":true,""id"":1(?<gpu>\d+))|(""id"":1(?<gpu>\d+),""result"":true)"; var regex = VirtualRoot.GetRegex(pattern); var match = regex.Match(line); Assert.IsTrue(match.Success); Assert.AreEqual("2", match.Groups["gpu"].Value); line = @"10:52:52:601 1158 buf: {""jsonrpc"":""2.0"",""id"":15,""result"":true}"; regex = VirtualRoot.GetRegex(pattern); match = regex.Match(line); Assert.IsTrue(match.Success); Assert.AreEqual("5", match.Groups["gpu"].Value); }
private static double PickUsdCny(string html) { try { double result = 0; var regex = VirtualRoot.GetRegex(@"CURRENCY_CONF\.usd_cny = Number\('(\d+\.?\d*)' \|\| \d+\.?\d*\);"); var matchs = regex.Match(html); if (matchs.Success) { double.TryParse(matchs.Groups[1].Value, out result); } return(result); } catch (Exception e) { Logger.ErrorDebugLine(e); return(0); } }
private static IncomeItem PickVDSIncomeItem(string vdsUUHtml, string vdsZtHtml, double usdCny) { const string pattern = "\"symbol\":\"vds\",.+,\"hr\":\"(?<netSpeed>[\\d\\.]+)\\s(?<netSpeedUnit>\\w+)\",\"est\":\"(?<incomeCoin>[\\d\\.]+) VDS\\\\/(?<speedUnit>\\w+)\","; IncomeItem result = new IncomeItem { CoinCode = "VDS", DataCode = "VDS", Speed = 1, }; var regex = VirtualRoot.GetRegex(pattern); var match = regex.Match(vdsUUHtml); if (match.Success) { string incomeCoinText = match.Groups["incomeCoin"].Value; result.SpeedUnit = match.Groups["speedUnit"].Value + "h/s"; string netSpeedText = match.Groups["netSpeed"].Value; result.NetSpeedUnit = match.Groups["netSpeedUnit"].Value + "h/s"; double incomeCoin; if (double.TryParse(incomeCoinText, out incomeCoin)) { result.IncomeCoin = incomeCoin; } double netSpeed; if (double.TryParse(netSpeedText, out netSpeed)) { result.NetSpeed = netSpeed; } } const string pattern1 = "\"VDS\",.+?,\"last\":\"(?<incomeCny>[\\d\\.]+)\""; regex = VirtualRoot.GetRegex(pattern1); match = regex.Match(vdsZtHtml); if (match.Success) { string incomeCnyText = match.Groups["incomeCny"].Value; double incomeCny; if (double.TryParse(incomeCnyText, out incomeCny)) { result.IncomeCny = incomeCny * result.IncomeCoin; result.IncomeUsd = result.IncomeCny / usdCny; } } return(result); }
public void RegexTest2() { string line = "Share accepted"; Regex regex = VirtualRoot.GetRegex(@"(Share accepted)|(GPU\s?(?<gpu>\d+).+\nShare accepted)"); var match = regex.Match(line); Assert.IsTrue(match.Success); string gpuText = match.Groups[NTKeyword.GpuIndexGroupName].Value; Assert.AreEqual("", gpuText); line = "GPU 1: this is a test\nShare accepted"; regex = VirtualRoot.GetRegex(@"(Share accepted)|(GPU\s?(?<gpu>\d+).+\nShare accepted)"); match = regex.Match(line); Assert.IsTrue(match.Success); gpuText = match.Groups[NTKeyword.GpuIndexGroupName].Value; Assert.AreEqual("1", gpuText); }
public void RegexPerfomanceTest() { string text = @"11:55:42:201 384 ETH: GPU0 14.015 Mh/s, GPU1 21.048 Mh/s"; NTStopwatch.Start(); Regex regex = new Regex(@"GPU(?<gpu>\d+) (?<gpuSpeed>[\d\.]+) (?<gpuSpeedUnit>.+?/s)"); for (int i = 0; i < 100; i++) { MatchCollection matches = regex.Matches(text); foreach (Match match in matches) { var a = match.Groups["gpu"]; var b = match.Groups["gpuSpeed"]; var c = match.Groups["gpuSpeedUnit"]; var d = match.Groups["notexit"]; } } var elapsedMilliseconds = NTStopwatch.Stop(); Console.WriteLine($"非编译:耗时{elapsedMilliseconds}"); string pattern = @"GPU(?<gpu>\d+) (?<gpuSpeed>[\d\.]+) (?<gpuSpeedUnit>.+?/s)"; for (int i = 0; i < 1000; i++) { VirtualRoot.GetRegex(Guid.NewGuid().ToString()); } NTStopwatch.Start(); for (int i = 0; i < 100; i++) { MatchCollection matches = VirtualRoot.GetRegex(pattern).Matches(text); foreach (Match match in matches) { var a = match.Groups["gpu"]; var b = match.Groups["gpuSpeed"]; var c = match.Groups["gpuSpeedUnit"]; var d = match.Groups["notexit"]; } } elapsedMilliseconds = NTStopwatch.Stop(); Console.WriteLine($"编译 :耗时{elapsedMilliseconds}"); }