-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMainWindow.xaml.cs
182 lines (167 loc) · 6.56 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using RegularTool.Model;
using RegularTool.ViewModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace RegularTool
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void BtnSearch_Click(object sender, RoutedEventArgs e)
{
txtContent.SelectionStart = 0;
txtContent.SelectAll();
txtContent.SelectionBackColor = System.Drawing.Color.White;
if (txtSearch.Text == "")
{
return;
}
int startIndex = 0;
while (startIndex < txtContent.TextLength)
{
int wordStartIndex = txtContent.Find(txtSearch.Text, startIndex, System.Windows.Forms.RichTextBoxFinds.None);
if (wordStartIndex != -1)
{
txtContent.SelectionStart = wordStartIndex;
txtContent.SelectionLength = txtSearch.Text.Length;
txtContent.SelectionBackColor = System.Drawing.Color.Yellow;
}
else
break;
startIndex += wordStartIndex + txtSearch.Text.Length;
}
}
private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
var contentModel = e.NewValue as GrammarModel;
if (contentModel != null && !contentModel.IsGrouping)
{
MainVm.Instance.SelectGrammar = contentModel;
tabOption.SelectedIndex = 0;
}
}
private void BtnMatch_Click(object sender, RoutedEventArgs e)
{
DataTable dataTable = new DataTable();
int count = 0;
tabOption.SelectedIndex = 2;
if (string.IsNullOrWhiteSpace(txtRegular.Text) || string.IsNullOrWhiteSpace(txtContent.Text))
{
dataResult.ItemsSource = dataTable.DefaultView;
statusMatchCount.Content = 0;
statusMatchSubCount.Content = 0;
return;
}
Regex regex = new Regex(txtRegular.Text, rbMulti.IsChecked == true ? RegexOptions.Multiline : rbSingle.IsChecked == true ? RegexOptions.Singleline : RegexOptions.IgnoreCase);
var result = regex.Matches(txtContent.Text);
if (result.Count == 0)
{
dataResult.ItemsSource = dataTable.DefaultView;
statusMatchCount.Content = 0;
statusMatchSubCount.Content = 0;
return;
}
List<List<string>> matchResults = new List<List<string>>();
foreach (var item in result)
{
var match = item as Match;
List<string> matchGroups = new List<string>();
for (int i = 0; i < match.Groups.Count; i++)
{
matchGroups.Add(match.Groups[i].Value);
}
matchResults.Add(matchGroups);
}
dataTable.Columns.Add("序号");
dataTable.Columns.Add("匹配文本");
count = matchResults.FirstOrDefault()?.Count ?? 0;
if (count > 0)
{
for (int i = 1; i < count; i++)
{
dataTable.Columns.Add("子表达式" + i);
}
}
for (int i = 0; i < matchResults.Count; i++)
{
var row = dataTable.NewRow();
for (int j = 0; j < matchResults[i].Count; j++)
{
if (j == 0)
{
row["序号"] = i + 1;
row["匹配文本"] = matchResults[i][j];
}
else
{
row["子表达式" + j] = matchResults[i][j];
}
}
dataTable.Rows.Add(row);
}
dataResult.ItemsSource = dataTable.DefaultView;
statusMatchCount.Content = matchResults.Count;
statusMatchSubCount.Content = count - 1;
}
private void BtnReplacce_Click(object sender, RoutedEventArgs e)
{
tabOption.SelectedIndex = 3;
if (string.IsNullOrWhiteSpace(txtRegular.Text) || string.IsNullOrWhiteSpace(txtContent.Text))
{
txtReplaceResult.Text = "";
return;
}
Regex regex = new Regex(txtRegular.Text, rbMulti.IsChecked == true ? RegexOptions.Multiline : rbSingle.IsChecked == true ? RegexOptions.Singleline : RegexOptions.IgnoreCase);
var isMatch = regex.IsMatch(txtContent.Text);
if (isMatch)
{
txtReplaceResult.Text = regex.Replace(txtContent.Text, txtReplace.Text);
}
else
{
txtReplaceResult.Text = "";
}
}
private void BtnGenerateCode_Click(object sender, RoutedEventArgs e)
{
tabOption.SelectedIndex = 4;
var code = "//matchResults为匹配到的结果,inputText为要匹配的内容";
code += "\r\nstring inputText = \"\";";
code += "\r\nRegex regex = new Regex(@\"" + txtRegular.Text + "\", " + (rbMulti.IsChecked == true ? "RegexOptions.Multiline" : rbSingle.IsChecked == true ? "RegexOptions.Singleline" : "RegexOptions.IgnoreCase") + ");";
code += "\r\nvar result = regex.Matches(inputText);";
code += "\r\nList<List<string>> matchResults = new List<List<string>>();";
code += "\r\nforeach (var item in result)" +
"\r\n{" +
"\r\n var match = item as Match;" +
"\r\n List<string> matchGroups = new List<string>();" +
"\r\n for (int i = 0; i < match.Groups.Count; i++)" +
"\r\n {" +
"\r\n matchGroups.Add(match.Groups[i].Value);" +
"\r\n }" +
"\r\n matchResults.Add(matchGroups);" +
"\r\n}";
txtCodeEdit.Text = code;
}
}
}