使用Winform编写Regex测试工具

前端之家收集整理的这篇文章主要介绍了使用Winform编写Regex测试工具前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


/// <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("\r\n",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("\r\n",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("\r\n");
}
}
if (lists.Count==0)
{
txtResult.Text = "不能匹配!";
}
else
{
txtResult.Text = string.Join("\r\n",lists);
}
}

以下是运行结果:

原文链接:https://www.f2er.com/regex/361788.html

猜你在找的正则表达式相关文章