Skip to content

Commit fc83368

Browse files
committed
Addf RegexSplitter
1 parent 78d4faf commit fc83368

File tree

2 files changed

+230
-0
lines changed

2 files changed

+230
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<Window x:Class="DataToolChain.RegexSplitter"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:local="clr-namespace:DataToolChain"
7+
mc:Ignorable="d"
8+
d:DataContext="{d:DesignInstance Type=local:RegexSplitterViewModel, IsDesignTimeCreatable=True}"
9+
Title="RegexSplitter" Height="650" Width="800">
10+
<Grid>
11+
<Grid.ColumnDefinitions>
12+
<ColumnDefinition Width="395*"/>
13+
<ColumnDefinition Width="5"/>
14+
<ColumnDefinition Width="424*"/>
15+
</Grid.ColumnDefinitions>
16+
<Grid.RowDefinitions>
17+
<RowDefinition Height="Auto"/>
18+
<RowDefinition Height="*"/>
19+
</Grid.RowDefinitions>
20+
21+
<StackPanel Background="Beige" Grid.ColumnSpan="3">
22+
<TextBlock TextWrapping="Wrap" Margin="5">(Top) Enter List of Regexes on the Left, and their corresponding replacements on the right (if no replacement value specified then will remove). (Bottom) Enter text on left, new text appears on right.</TextBlock>
23+
<StackPanel Margin="5">
24+
25+
<StackPanel Orientation="Horizontal">
26+
<CheckBox IsChecked="{Binding Path=IsCaseSensitive}">Match Case</CheckBox>
27+
<CheckBox IsChecked="{Binding Path=RemoveEmpty}" Margin="10,0,0,0">Remove empty entries</CheckBox>
28+
</StackPanel>
29+
30+
<StackPanel Orientation="Horizontal">
31+
<CheckBox IsChecked="{Binding Path=UseRegex}">Use Regex</CheckBox>
32+
<CheckBox IsChecked="{Binding Path=Multiline}" Margin="10,0,0,0">Match newline</CheckBox>
33+
</StackPanel>
34+
</StackPanel>
35+
</StackPanel>
36+
37+
<GridSplitter Grid.Column="1" Grid.Row="1" Background="Gray" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></GridSplitter>
38+
39+
<Grid Grid.Column="0" Row="1">
40+
<Grid.RowDefinitions>
41+
<RowDefinition Height="Auto"/>
42+
<RowDefinition Height="5"/>
43+
<RowDefinition Height="410*"/>
44+
</Grid.RowDefinitions>
45+
<TextBox AcceptsReturn="False" AcceptsTab="True" TextWrapping="NoWrap" MaxLines="1" Padding="5" Text="{Binding Path=RegexMatchInputs, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
46+
<GridSplitter Grid.Row="1" Background="Gray" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></GridSplitter>
47+
<TextBox AcceptsReturn="True" Grid.Row="2" AcceptsTab="True" TextWrapping="NoWrap" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Text="{Binding Path=StringInput, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
48+
</Grid>
49+
50+
<TextBox AcceptsReturn="True" Grid.Row="1" Grid.Column="2" AcceptsTab="True" TextWrapping="NoWrap" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Text="{Binding Path=StringOutput, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
51+
52+
<!--<Grid Grid.Column="0" Grid.Row="1">
53+
<Grid.RowDefinitions>
54+
<RowDefinition Height="Auto"/>
55+
<RowDefinition Height="5"/>
56+
<RowDefinition Height="410*"/>
57+
</Grid.RowDefinitions>
58+
<TextBox AcceptsReturn="True" AcceptsTab="True" TextWrapping="NoWrap" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Text="{Binding Path=RegexMatchInputs, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
59+
<GridSplitter Grid.Column="1" Background="Gray" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></GridSplitter>
60+
61+
</Grid>-->
62+
63+
</Grid>
64+
</Window>
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
using System;
2+
using System.ComponentModel;
3+
using System.Linq;
4+
using System.Runtime.CompilerServices;
5+
using System.Text.RegularExpressions;
6+
using System.Windows;
7+
8+
namespace DataToolChain
9+
{
10+
/// <summary>
11+
/// Interaction logic for RegexMatcher.xaml
12+
/// </summary>
13+
public partial class RegexSplitter : Window
14+
{
15+
public RegexSplitterViewModel _viewModel { get; set; } = new RegexSplitterViewModel();
16+
17+
public RegexSplitter()
18+
{
19+
InitializeComponent();
20+
21+
this.DataContext = _viewModel;
22+
}
23+
}
24+
25+
26+
public class RegexSplitterViewModel : INotifyPropertyChanged
27+
{
28+
private string _stringOutput;
29+
private string _stringInput = @"The quick brown fox jumped over the lazy dog";
30+
private string _regexMatchInputs = @"\W+";
31+
32+
private bool _useRegex = true;
33+
private bool _isCaseSensitive = false;
34+
private bool _multiline;
35+
private bool _removeEmpty;
36+
public event PropertyChangedEventHandler PropertyChanged;
37+
38+
public bool IsCaseSensitive
39+
{
40+
get { return _isCaseSensitive; }
41+
set
42+
{
43+
_isCaseSensitive = value;
44+
UpdateOutput();
45+
}
46+
}
47+
48+
/// <summary>
49+
/// A little confusing, but when this is true it sets regex Multiline option = false.
50+
/// </summary>
51+
public bool RemoveEmpty
52+
{
53+
get { return _removeEmpty; }
54+
set
55+
{
56+
_removeEmpty = value;
57+
UpdateOutput();
58+
}
59+
}
60+
61+
62+
/// <summary>
63+
/// A little confusing, but when this is true it sets regex Multiline option = false.
64+
/// </summary>
65+
public bool Multiline
66+
{
67+
get { return _multiline; }
68+
set
69+
{
70+
_multiline = value;
71+
UpdateOutput();
72+
}
73+
}
74+
75+
public bool UseRegex
76+
{
77+
get { return _useRegex; }
78+
set
79+
{
80+
_useRegex = value;
81+
UpdateOutput();
82+
}
83+
}
84+
85+
86+
public RegexOptions GetRegexOptions()
87+
{
88+
var regexOptions = RegexOptions.None;
89+
90+
if (Multiline == false)
91+
{
92+
regexOptions |= RegexOptions.Multiline;
93+
}
94+
else
95+
{
96+
regexOptions |= RegexOptions.Singleline;
97+
}
98+
99+
if (IsCaseSensitive == false)
100+
regexOptions |= RegexOptions.IgnoreCase;
101+
102+
return regexOptions;
103+
}
104+
105+
public string RegexMatchInputs
106+
{
107+
get { return _regexMatchInputs; }
108+
set
109+
{
110+
_regexMatchInputs = value;
111+
UpdateOutput();
112+
}
113+
}
114+
115+
public string StringInput
116+
{
117+
get { return _stringInput; }
118+
set
119+
{
120+
_stringInput = value;
121+
UpdateOutput();
122+
}
123+
}
124+
125+
public string StringOutput
126+
{
127+
get { return _stringOutput; }
128+
set
129+
{
130+
_stringOutput = value;
131+
OnPropertyChanged();
132+
}
133+
}
134+
135+
public RegexSplitterViewModel()
136+
{
137+
UpdateOutput();
138+
}
139+
140+
public void UpdateOutput()
141+
{
142+
var matchesInput = RegexMatchInputs;
143+
144+
try
145+
{
146+
var regexOptions = GetRegexOptions();
147+
148+
var splitResults = Regex.Split(StringInput, UseRegex ? matchesInput : Regex.Escape(matchesInput), regexOptions, TimeSpan.FromMinutes(1));
149+
150+
StringOutput = string.Join("\r\n", RemoveEmpty ? splitResults.Where(txt => string.IsNullOrWhiteSpace(txt) == false) : splitResults);
151+
}
152+
catch (Exception)
153+
{
154+
StringOutput = "Error in regex.";
155+
return;
156+
}
157+
}
158+
159+
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
160+
{
161+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
162+
}
163+
164+
}
165+
}
166+

0 commit comments

Comments
 (0)