Skip to content

Commit fc39e04

Browse files
committed
Add string column builder
1 parent fc83368 commit fc39e04

File tree

2 files changed

+201
-0
lines changed

2 files changed

+201
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<Window x:Class="DataToolChain.StringColumnBuilder"
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:StringColumnBuilderViewModel, IsDesignTimeCreatable=True}"
9+
Title="StringColumnBuilder" Height="650" Width="800">
10+
<Grid>
11+
<Grid.ColumnDefinitions>
12+
<ColumnDefinition Width="395*"/>
13+
<ColumnDefinition Width="5"/>
14+
<ColumnDefinition Width="424*"/>
15+
<ColumnDefinition Width="5"/>
16+
<ColumnDefinition Width="424*"/>
17+
</Grid.ColumnDefinitions>
18+
<Grid.RowDefinitions>
19+
<RowDefinition Height="Auto"/>
20+
<RowDefinition Height="*"/>
21+
</Grid.RowDefinitions>
22+
23+
<StackPanel Background="Beige" Grid.ColumnSpan="5">
24+
<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>
25+
<StackPanel Margin="5">
26+
27+
<StackPanel Orientation="Horizontal">
28+
<CheckBox IsChecked="{Binding Path=IsCaseSensitive}">Match Case</CheckBox>
29+
<CheckBox IsChecked="{Binding Path=RemoveEmpty}" Margin="10,0,0,0">Remove empty entries</CheckBox>
30+
</StackPanel>
31+
32+
<StackPanel Orientation="Horizontal">
33+
<CheckBox IsChecked="{Binding Path=UseRegex}">Use Regex</CheckBox>
34+
<CheckBox IsChecked="{Binding Path=Multiline}" Margin="10,0,0,0">Match newline</CheckBox>
35+
</StackPanel>
36+
</StackPanel>
37+
</StackPanel>
38+
39+
<GridSplitter Grid.Column="1" Grid.Row="1" Background="Gray" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></GridSplitter>
40+
41+
<Grid Grid.Column="0" Row="1">
42+
<Grid.RowDefinitions>
43+
<RowDefinition Height="Auto"/>
44+
<RowDefinition Height="5"/>
45+
<RowDefinition Height="410*"/>
46+
</Grid.RowDefinitions>
47+
<TextBox AcceptsReturn="False" AcceptsTab="True" TextWrapping="NoWrap" MaxLines="1" Padding="5" Text="{Binding Path=Separator, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
48+
<GridSplitter Grid.Row="1" Background="Gray" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></GridSplitter>
49+
<TextBox AcceptsReturn="True" Grid.Row="2" AcceptsTab="True" TextWrapping="NoWrap" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Text="{Binding Path=StringInput, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
50+
</Grid>
51+
52+
<TextBox AcceptsReturn="True" Grid.Row="1" Grid.Column="2" AcceptsTab="True" TextWrapping="NoWrap" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Text="{Binding Path=ExistingData, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
53+
54+
<GridSplitter Grid.Column="3" Grid.Row="1" Background="Gray" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></GridSplitter>
55+
56+
<TextBox AcceptsReturn="True" Grid.Row="1" Grid.Column="4" AcceptsTab="True" TextWrapping="NoWrap" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Text="{Binding Path=StringOutput, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
57+
58+
<!--<Grid Grid.Column="0" Grid.Row="1">
59+
<Grid.RowDefinitions>
60+
<RowDefinition Height="Auto"/>
61+
<RowDefinition Height="5"/>
62+
<RowDefinition Height="410*"/>
63+
</Grid.RowDefinitions>
64+
<TextBox AcceptsReturn="True" AcceptsTab="True" TextWrapping="NoWrap" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Text="{Binding Path=RegexMatchInputs, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
65+
<GridSplitter Grid.Column="1" Background="Gray" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></GridSplitter>
66+
67+
</Grid>-->
68+
69+
</Grid>
70+
</Window>
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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 StringColumnBuilder : Window
14+
{
15+
public StringColumnBuilderViewModel _viewModel { get; set; } = new StringColumnBuilderViewModel();
16+
17+
public StringColumnBuilder()
18+
{
19+
InitializeComponent();
20+
21+
this.DataContext = _viewModel;
22+
}
23+
}
24+
25+
26+
public class StringColumnBuilderViewModel : INotifyPropertyChanged
27+
{
28+
private string _stringOutput;
29+
private string _stringInput = @"red
30+
blue
31+
cyan
32+
magenta";
33+
private string _existingData = @"apple
34+
dog
35+
cat
36+
fruit";
37+
private string _separator = @"\t";
38+
39+
private bool _useRegex = true;
40+
private bool _isCaseSensitive = false;
41+
private bool _multiline;
42+
private bool _removeEmpty;
43+
public event PropertyChangedEventHandler PropertyChanged;
44+
45+
public bool UseRegex
46+
{
47+
get { return _useRegex; }
48+
set
49+
{
50+
_useRegex = value;
51+
UpdateOutput();
52+
}
53+
}
54+
55+
public string Separator
56+
{
57+
get { return _separator; }
58+
set
59+
{
60+
_separator = value;
61+
UpdateOutput();
62+
}
63+
}
64+
65+
public string StringInput
66+
{
67+
get { return _stringInput; }
68+
set
69+
{
70+
_stringInput = value;
71+
UpdateOutput();
72+
}
73+
}
74+
75+
public string ExistingData
76+
{
77+
get { return _existingData; }
78+
set
79+
{
80+
_existingData = value;
81+
UpdateOutput();
82+
}
83+
}
84+
85+
public string StringOutput
86+
{
87+
get { return _stringOutput; }
88+
set
89+
{
90+
_stringOutput = value;
91+
OnPropertyChanged();
92+
}
93+
}
94+
95+
public StringColumnBuilderViewModel()
96+
{
97+
UpdateOutput();
98+
}
99+
100+
private static readonly Regex LinesRegex = new Regex("\r\n?", RegexOptions.Compiled);
101+
102+
public void UpdateOutput()
103+
{
104+
var matchesInput = Separator;
105+
106+
try
107+
{
108+
//loop through existing data and append input into output
109+
110+
var inputLines = LinesRegex.Split(StringInput);
111+
var dataLines = LinesRegex.Split(ExistingData);
112+
113+
var sepString = UseRegex ? Regex.Unescape(Separator) : Separator;
114+
115+
StringOutput = string.Join("\r\n", inputLines.Zip(dataLines, (input, data) => input + sepString + data));
116+
}
117+
catch (Exception)
118+
{
119+
StringOutput = "Error in regex.";
120+
return;
121+
}
122+
}
123+
124+
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
125+
{
126+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
127+
}
128+
129+
}
130+
}
131+

0 commit comments

Comments
 (0)