using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Threading;
using System.IO;
using MyHelper.TextHelper;
namespace RegexTest
{
public partial class RegexForm : FYJ.Winform.Forms.BaseForm
{
public RegexForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Match match = Regex.Match(this.richTextBox1.Text, this.richTextBox2.Text, RegexOptions.IgnoreCase);
if (match != null)
{
this.richTextBox3.Text = match.Value;
}
else
{
this.richTextBox3.Text = "";
MessageBox.Show("匹配失败");
}
}
private void button2_Click(object sender, EventArgs e) //提取
{
MatchCollection matchs = Regex.Matches(this.richTextBox1.Text, this.richTextBox2.Text, RegexOptions.IgnoreCase);
if (matchs != null && matchs.Count > 0)
{
string lines = "";
int groupCount = 0;
for (int i = 0; i < matchs.Count; i++)
{
string cols = "";
Match match = matchs[i];
for (int j = 0; j < match.Groups.Count; j++)
{
if (match.Groups.Count == 1) //如果只有一个分组 即匹配的所有项之一
{
cols += match.Groups[j].Value + " ";
}
else
{
if (j > 0)
{
cols += match.Groups[j].Value + " ";
}
}
}
groupCount = match.Groups.Count;
lines += cols + Environment.NewLine;
}
this.label_status.Text = matchs.Count + "行," + groupCount+"组";
this.richTextBox3.Text = lines;
}
else
{
this.richTextBox3.Text = "";
MessageBox.Show("匹配失败");
}
}
private void btn浏览_Click(object sender, EventArgs e)
{
using (OpenFileDialog of = new OpenFileDialog())
{
if (of.ShowDialog() == DialogResult.OK)
{
Encoding encoding = EncodingHelper.GetEncoding(of.FileName);
StreamReader stream = new StreamReader(of.FileName, encoding);
this.richTextBox1.Text = stream.ReadToEnd();
stream.Close();
}
}
}
}
}