加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用Winform编写Regex测试工具

发布时间:2020-12-14 01:47:11 所属栏目:百科 来源:网络整理
导读:?? /// summary /// 运行匹配 /// /summary /// param name="sender"/param /// param name="e"/param private void btnRunMatching_Click(object sender,EventArgs e) { bool result = Regex.IsMatch(txtContent.Text,txtRegex.Text); if (result) { txtRes
??

/// <summary>
/// 运行匹配
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnRunMatching_Click(object sender,EventArgs e)
{
bool result = Regex.IsMatch(txtContent.Text,txtRegex.Text);
if (result)
{
txtResult.Text = "能匹配成功!";
}
else
{
txtResult.Text = "不能匹配!";
}
}

/// <summary>
/// 开始匹配
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnStartMatching_Click(object sender,EventArgs e)
{
MatchCollection mcn = Regex.Matches(txtContent.Text,txtRegex.Text);
List<string> liStr = new List<string>();
for (int i = 0; i < mcn.Count; i++)
{
Match m = mcn[i];
if (m.Success)
{
liStr.Add(string.Format("{0}",m.Value));
}
}
if (liStr.Count==0)
{
txtResult.Text = "不能匹配!";
}
else
{
txtResult.Text = string.Join("rn",liStr);
}
}

/// <summary>
/// 运行提取单
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnRunExtract_Click(object sender,EventArgs e)
{
Match m = Regex.Match(txtContent.Text,txtRegex.Text);
// Match的常用属性
if (m.Success)
{
List<string> list = new List<string>();
list.Add(string.Format("匹配到的结果是{0}",m.Groups[0].Value));
list.Add(string.Format("匹配到的字符串在原始字符串中的索引是{0}",m.Index));
list.Add(string.Format("匹配到的字符串的长度是{0}",m.Length));
txtResult.Text = string.Join("rn",list);
}
else
{
txtResult.Text = "不能匹配!";
}
}

/// <summary>
/// 运行提取多
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnRunReplace_Click(object sender,EventArgs e)
{
MatchCollection mc = Regex.Matches(txtContent.Text,txtRegex.Text);
List<string> lists = new List<string>();
for (int i = 0; i < mc.Count; i++)
{
Match m = mc[i];
if (m.Success)
{
lists.Add(string.Format("匹配到的结果是{0}",m.Value));
lists.Add(string.Format("匹配到的字符串在原始字符串中的索引是{0}",m.Index));
lists.Add(string.Format("匹配到的字符串的长度是{0}",m.Length));
lists.Add("rn");
}
}
if (lists.Count==0)
{
txtResult.Text = "不能匹配!";
}
else
{
txtResult.Text = string.Join("rn",lists);
}
}

以下是运行结果:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读