//获取正则表达式的匹配参数
private RegexOptions GetSelectedRegexOptions()
{
RegexOptions selectedRegexOptions = RegexOptions.None;
if (this.IgnoreCaseChkBox.Checked)
selectedRegexOptions |= RegexOptions.IgnoreCase;
if (this.ExplicitCaptureChkBox.Checked)
selectedRegexOptions |= RegexOptions.ExplicitCapture;
if (this.ECMAScriptChkBox.Checked)
selectedRegexOptions |= RegexOptions.ECMAScript;
if (this.IgnoreCaseChkBox.Checked)
selectedRegexOptions |= RegexOptions.IgnoreCase;
if (this.MultiLineChkBox.Checked)
selectedRegexOptions |= RegexOptions.Multiline;
if (this.RightToLeftChkBox.Checked)
selectedRegexOptions |= RegexOptions.RightToLeft;
if (this.SingeLineChkBox.Checked)
selectedRegexOptions |= RegexOptions.Singleline;
return selectedRegexOptions;
}
private void TestRegexButton_Click(object sender, EventArgs e)
{
try
{
RegexOptions selectedRegexOptions = this.GetSelectedRegexOptions();
Regex testRegex = new Regex(this.RegexTextBox.Text, selectedRegexOptions);
if (testRegex.IsMatch(this.InputTextBox.Text))
{
this.ResultsTextBox.ForeColor = Color.Black;
this.ResultsTextBox.Text = "匹配成功";
}
else
{
this.ResultsTextBox.ForeColor = Color.Red;
this.ResultsTextBox.Text = "匹配失败";
}
}
catch (ArgumentException ex)
{
this.ResultsTextBox.ForeColor = Color.Red;
this.ResultsTextBox.Text = "错误:"+ex.Message;
}
}
private void ReplaceButton_Click(object sender, EventArgs e)
{
try
{
RegexOptions selectedRegexOptions = this.GetSelectedRegexOptions();
Regex replaceRegex = new Regex(this.RegexTextBox.Text, selectedRegexOptions);
this.ResultsTextBox.ForeColor = Color.Black;
this.ResultsTextBox.Text = replaceRegex.Replace(this.InputTextBox.Text, this.ReplacementTextBox.Text);
}
catch (ArgumentException ex)
{
this.ResultsTextBox.ForeColor = Color.Red;
this.ResultsTextBox.Text = "错误:" + ex.Message;
}
}
private void SplitBoutton_Click(object sender, EventArgs e)
{
try
{
RegexOptions selectedRegexOptions = this.GetSelectedRegexOptions();
Regex splitRegex = new Regex(this.RegexTextBox.Text, selectedRegexOptions);
String[] splitResults;
splitResults = splitRegex.Split(this.InputTextBox.Text);
StringBuilder resultsString = new StringBuilder(this.InputTextBox.Text.Length);
foreach (String stringElement in splitResults)
resultsString.Append(stringElement + Environment.NewLine);
this.ResultsTextBox.ForeColor = Color.Black;
this.ResultsTextBox.Text = resultsString.ToString();
}
catch (ArgumentException ex)
{
this.ResultsTextBox.ForeColor = Color.Red;
this.ResultsTextBox.Text = "错误:" + ex.Message;
}
}
private void MatchesButton_Click(object sender, EventArgs e)
{
try
{
RegexOptions selectedRegexOptions = this.GetSelectedRegexOptions();
Regex matchesRegex = new Regex(this.RegexTextBox.Text, selectedRegexOptions);
MatchCollection matchesFound;
matchesFound = matchesRegex.Matches(this.InputTextBox.Text);
String nextMath = "------- 下一个匹配项 ---------- ";
StringBuilder resultsString = new StringBuilder(64);
foreach(Match matchMode in matchesFound)
resultsString.Append(matchMode.Value +(Environment.NewLine+nextMath));
this.ResultsTextBox.ForeColor = Color.Black;
this.ResultsTextBox.Text = resultsString.ToString();
}
catch (ArgumentException ex)
{
this.ResultsTextBox.ForeColor = Color.Red;
this.ResultsTextBox.Text = "错误:" + ex.Message;
}
}
private void GroupsButton_Click(object sender, EventArgs e)
{
try
{
RegexOptions selectedRegexOptions = this.GetSelectedRegexOptions();
Regex matchesRegex = new Regex(this.RegexTextBox.Text, selectedRegexOptions);
MatchCollection matchesFound;
matchesFound = matchesRegex.Matches(this.InputTextBox.Text);
String nextMath = "------- 下一个分组 ---------- ";
StringBuilder resultsString = new StringBuilder(64);
GroupCollection matchGroups;
foreach (Match matchMode in matchesFound)
{
matchGroups = matchMode.Groups;
foreach (Group matchGroup in matchGroups)
resultsString.Append("(" + matchGroup.Value + ")");
resultsString.Append(Environment.NewLine + nextMath);
}
this.ResultsTextBox.ForeColor = Color.Black;
this.ResultsTextBox.Text = resultsString.ToString();
}
catch (ArgumentException ex)
{
this.ResultsTextBox.ForeColor = Color.Red;
this.ResultsTextBox.Text = "错误:" + ex.Message;
}
}
//常见正则表达式
private void FamiliarRegex_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
MenuRegex.Show(this,new Point(FamiliarRegex.Location.X, FamiliarRegex.Location.Y + FamiliarRegex.Size.Height));
//MessageBox.Show("sdfsd");
}
private void MenuRegexItem_Click(object sender, EventArgs e)
{
string strRegex = "";
switch (((ToolStripMenuItem)sender).Text)
{
case "整数":
strRegex = @"^((+|-)d)?d*$";
break;
case "浮点数":
strRegex = @"^(?:+|-)?d+(?:.d+)?$";
break;
case "电话号码":
strRegex = @"d{3}-d{8}|d{4}-d{7}";
break;
case "邮政编码":
strRegex = @"[1-9]d{5}(?!d)";
break;
case "Email地址1":
strRegex = @"^(([^<>()[]\.,;:@"+'"'+@"