private bool CheckData() { ErrMsg = string.Empty; try { var result = ReflectionHelper.ChangeType(Value, ParaType); if (Value.IsNullOrEmpty() || result == null) { if (!CanBeNull) { ErrMsg += "参数{0}不可为空!".FormatWith(Name); } } } catch { ErrMsg += "参数{0}类型错误!".FormatWith(Name); } if (ErrMsg.IsNullOrEmpty()) { if (RegexText.IsNotNullOrEmpty() && !Value.As <IRegex>().IsMatch(RegexText)) { ErrMsg += "参数{0}值不匹配![{1}]".FormatWith(Name, RegexText); } if (_minLength >= 0 && Value.Length < _minLength) { ErrMsg += "参数{0}小于最小长度{1}".FormatWith(Name, _minLength); } if (_maxLength >= 0 && Value.Length > _maxLength) { ErrMsg += "参数{0}大于最大长度{1}".FormatWith(Name, _minLength); } if (_minValue > 0 && Value.As <IConvert>().ToFloat(0) < _minValue) { ErrMsg += "参数{0}小于最小值{1}".FormatWith(Name, _minValue); } if (_maxValue > 0 && Value.As <IConvert>().ToFloat(-1) > _maxValue) { ErrMsg += "参数{0}大于最大值{1}".FormatWith(Name, _maxValue); } } return(ErrMsg.IsNullOrEmpty()); }
protected override void OnClosing(CancelEventArgs e) { //通知UniqueAsyncProcess结束任务 UniqueAsyncProcess.RequireEnd(); //询问是否返回数据 if (textBox2.Text.Length > 0) { DialogResult = MessageBox.Show("是否应用标注?", Text, MessageBoxButtons.YesNoCancel); switch (DialogResult) { case DialogResult.Cancel: e.Cancel = true; break; case DialogResult.Yes: try { string[] patterns = RegexText.GetLines(); MatchCollection matches = Regex.Matches(FileContext, patterns[0]); Annotations = GetAnnoFromRegex(brushListPanel.CurrentItem, matches.ToStringArray(), patterns.SubArray(1, patterns.Length - 1), brushListPanel.CurrentItem.AnnoType.GetFields(FieldsOrder.BaseToSub), progress: new GlobalMessage.Progress() { Print = PrintStatus, ProgressingFormatString = "计算中,完成{0}%" }); } catch (Exception ex) { MessageBox.Show("正则匹配错误:" + ex.Message, ex.Source); e.Cancel = true; } break; case DialogResult.No: break; } } base.OnClosing(e); }
public void HasValTest() { string pattern = @"@@@(\d{1}),"; var s = "HJ26440601099302019-07-18 13:17:19@@@2019-07-18 13:17:19.193tek1d####"; var b = RegexText.HasVal(s, pattern); Assert.False(b); s = "HJ26440601099302019-07-18 14:49:155fb265d5-0a0e-4359-8eb1-ecc187c49502@@@0,未进行连接注册tek74####"; b = RegexText.HasVal(s, pattern); var t = RegexText.MatchVal(s, pattern); Assert.True(b); s = "HJ26440601099302019-07-18 13:21:42d06b8091-791a-4c05-a87a-4eb5978c7c57@@@1,2019-07-18 13:21:42tek13####"; b = RegexText.HasVal(s, pattern); t = RegexText.MatchVal(s, pattern); s = "HJ26440601099302019-07-18 13:17:19@@@2019-07-18 13:17:19.193tek1d####"; pattern = "@@@(.*)tek"; t = RegexText.MatchVal(s, pattern, 1); Assert.True(b); }
public void TestMatch01() { string s = "2020/05/09 16:21:19 _[接收]_<?xml version=\"1.0\" encoding=\"GBK\"?></Result><Dev><FQYNAME></FQYNAME><FQYMODEL></FQYMODEL><YDJNAME>不透光烟度计</YDJNAME><YDJMODEL>MQY-202</YDJMODEL><RPMNAME>鸣泉转速计</RPMNAME><RPMMODEL>MQZ-4</RPMMODEL><NOxNAME>排放气体测试仪</NOxNAME><NOxMODEL>MQW-5102</NOxMODEL><CDMNAME>测功机</CDMNAME><CDMMODEL>CDM-300C</CDMMODEL><START_TIME>2020-05-09 16:21:19</START_TIME></Dev></Request>"; RegexText.MatchVal(s, @"_\[接收\]_(.*)", 2); }
/// <summary> /// Determines type of the content. /// </summary> /// <param name="cellValue">The cell value.</param> /// <returns></returns> public CellContentInfo DetermineContentType(String cellValue, Boolean UseNoDataWillCards = true) { CellContentInfo output = new CellContentInfo(); String input = cellValue; input = valueExtraction.ProcessInput(cellValue, null, null); output.content = input; if (input.isNullOrEmpty()) { output.type = CellContentType.empty; return(output); } else { output.length = input.Length; } if (input == FORMAT_WILLCARD) { output.type = CellContentType.Any; } if (RegexText.IsMatch(input)) { output.type = CellContentType.textual; } else if (RegexFormatedNumber.IsMatch(input)) { if (_select_FORMAT_BANKACCOUNTNUMBER.IsMatch(input)) { output.type = CellContentType.bank_account; } else if (_select_DATE_UNIVERSAL.IsMatch(input)) { output.type = CellContentType.date_format; } else { output.type = CellContentType.numeric; if (input.Contains(".")) { output.type |= CellContentType.withDot; if (input.ToArray().Count(x => x == '.') > 1) { // output.type |= CellContentType.formatted; } } if (input.Contains(",")) { output.type |= CellContentType.withComma; if (input.ToArray().Count(x => x == ',') > 1) { output.type |= CellContentType.formatted; } } if (input.Contains("-")) { output.type |= CellContentType.withMinus; if (input.ToArray().Count(x => x == '-') > 1) { output.type |= CellContentType.formatted; } } if (input.Contains(" ")) { output.type |= CellContentType.withSpace; if (input.ToArray().Count(x => x == ' ') > 1) { output.type |= CellContentType.formatted; } } if (input.Contains("+")) { output.type |= CellContentType.withPlus; } if (input.Contains("%")) { output.type |= CellContentType.withPercentage; } if (input.StartsWith("0")) { if (input.Length > 1) { var mc = RegexSelectCleanNumber.Match(input.Substring(1)); if (mc.Success) { if (mc.Index == 0) { output.type |= CellContentType.formatted; } } } } if (input.Contains(Environment.NewLine)) { output.type |= CellContentType.formatted; } } } else { output.type = CellContentType.mixed; } return(output); }
/// <summary> /// 计算正则表达式并写入textBox3中 /// </summary> /// <param name="abort"></param> private void CalRegex(Process.Abort abort) { try { if (textBox2.Text.Length > 0) { //显示的最大匹配数 const int max = 100; //报告相关 GlobalMessage.Add("status", "计算正则..."); string[] patterns = RegexText.GetLines(); MatchCollection matches = Regex.Matches(FileContext, patterns[0]); StringBuilder matchResult = new StringBuilder(""); int realCount = Math.Min(max, matches.Count); //报告相关 GlobalMessage.Progress progress = new GlobalMessage.Progress(realCount) { ProgressedString = matches.Count > max ? $"就绪 {realCount}/{matches.Count}个匹配项" : $"就绪 {matches.Count}个匹配项" }; if (matches.Count > max) { GlobalMessage.Add("info delay", $"匹配项过多,加载前{max}项"); } ListViewGroup[] groups = new ListViewGroup[realCount]; for (int i = 0; i < realCount; i++) { if (abort != null && abort()) { throw new Process.ProcessAbortException(); } matchResult.Append(matches[i].Value.Replace("\n", @"\n") + "\r\n"); FieldInfo[] fields = brushListPanel.CurrentItem.AnnoType.GetFields(FieldsOrder.BaseToSub); AnnotationBase[] annos = GetAnnoFromRegex(brushListPanel.CurrentItem, new string[1] { matches[i].Value }, patterns.SubArray(1, patterns.Length - 1), fields, abort); if (annos == null) { throw new Process.ProcessAbortException(); } ListViewGroup group = new ListViewGroup($"Match {i} ({annos.Length})"); foreach (AnnotationBase anno in annos) { object[] values = anno.GetFieldsValues(fields); group.Items.Add(new ListViewItem(values.ToStringArray())); } groups[i] = group; //报告相关 progress.Report(i + 1); } //报告相关 if (realCount == 0) { progress.Report(progress.MaxValue); } Invoke(new Action(() => { textBox3.Text = matchResult.ToString().Substring(0, Math.Min(textBox3.MaxLength, matchResult.Length)); listView1.Groups.AddRange(groups); foreach (ListViewGroup group in groups) { listView1.Items.AddRange(group.Items); } })); } else { GlobalMessage.Add("status", "就绪"); } } catch (Process.ProcessAbortException) { } catch (Exception ex) { GlobalMessage.Add("status", "正则计算错误"); GlobalMessage.Add("exception delay", ex.Message); } finally { GC.Collect(); } }