Skip to content

Commit

Permalink
Addf RegexSplitter
Browse files Browse the repository at this point in the history
  • Loading branch information
nh43de committed Mar 13, 2023
1 parent 78d4faf commit fc83368
Show file tree
Hide file tree
Showing 2 changed files with 230 additions and 0 deletions.
64 changes: 64 additions & 0 deletions DataToolChain.Ui/RegexSplitter/RegexSplitter.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<Window x:Class="DataToolChain.RegexSplitter"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DataToolChain"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=local:RegexSplitterViewModel, IsDesignTimeCreatable=True}"
Title="RegexSplitter" Height="650" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="395*"/>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="424*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<StackPanel Background="Beige" Grid.ColumnSpan="3">
<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>
<StackPanel Margin="5">

<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding Path=IsCaseSensitive}">Match Case</CheckBox>
<CheckBox IsChecked="{Binding Path=RemoveEmpty}" Margin="10,0,0,0">Remove empty entries</CheckBox>
</StackPanel>

<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding Path=UseRegex}">Use Regex</CheckBox>
<CheckBox IsChecked="{Binding Path=Multiline}" Margin="10,0,0,0">Match newline</CheckBox>
</StackPanel>
</StackPanel>
</StackPanel>

<GridSplitter Grid.Column="1" Grid.Row="1" Background="Gray" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></GridSplitter>

<Grid Grid.Column="0" Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="5"/>
<RowDefinition Height="410*"/>
</Grid.RowDefinitions>
<TextBox AcceptsReturn="False" AcceptsTab="True" TextWrapping="NoWrap" MaxLines="1" Padding="5" Text="{Binding Path=RegexMatchInputs, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
<GridSplitter Grid.Row="1" Background="Gray" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></GridSplitter>
<TextBox AcceptsReturn="True" Grid.Row="2" AcceptsTab="True" TextWrapping="NoWrap" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Text="{Binding Path=StringInput, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
</Grid>

<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>

<!--<Grid Grid.Column="0" Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="5"/>
<RowDefinition Height="410*"/>
</Grid.RowDefinitions>
<TextBox AcceptsReturn="True" AcceptsTab="True" TextWrapping="NoWrap" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Text="{Binding Path=RegexMatchInputs, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
<GridSplitter Grid.Column="1" Background="Gray" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></GridSplitter>
</Grid>-->

</Grid>
</Window>
166 changes: 166 additions & 0 deletions DataToolChain.Ui/RegexSplitter/RegexSplitter.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
using System;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Windows;

namespace DataToolChain
{
/// <summary>
/// Interaction logic for RegexMatcher.xaml
/// </summary>
public partial class RegexSplitter : Window
{
public RegexSplitterViewModel _viewModel { get; set; } = new RegexSplitterViewModel();

public RegexSplitter()
{
InitializeComponent();

this.DataContext = _viewModel;
}
}


public class RegexSplitterViewModel : INotifyPropertyChanged
{
private string _stringOutput;
private string _stringInput = @"The quick brown fox jumped over the lazy dog";
private string _regexMatchInputs = @"\W+";

private bool _useRegex = true;
private bool _isCaseSensitive = false;
private bool _multiline;
private bool _removeEmpty;
public event PropertyChangedEventHandler PropertyChanged;

public bool IsCaseSensitive
{
get { return _isCaseSensitive; }
set
{
_isCaseSensitive = value;
UpdateOutput();
}
}

/// <summary>
/// A little confusing, but when this is true it sets regex Multiline option = false.
/// </summary>
public bool RemoveEmpty
{
get { return _removeEmpty; }
set
{
_removeEmpty = value;
UpdateOutput();
}
}


/// <summary>
/// A little confusing, but when this is true it sets regex Multiline option = false.
/// </summary>
public bool Multiline
{
get { return _multiline; }
set
{
_multiline = value;
UpdateOutput();
}
}

public bool UseRegex
{
get { return _useRegex; }
set
{
_useRegex = value;
UpdateOutput();
}
}


public RegexOptions GetRegexOptions()
{
var regexOptions = RegexOptions.None;

if (Multiline == false)
{
regexOptions |= RegexOptions.Multiline;
}
else
{
regexOptions |= RegexOptions.Singleline;
}

if (IsCaseSensitive == false)
regexOptions |= RegexOptions.IgnoreCase;

return regexOptions;
}

public string RegexMatchInputs
{
get { return _regexMatchInputs; }
set
{
_regexMatchInputs = value;
UpdateOutput();
}
}

public string StringInput
{
get { return _stringInput; }
set
{
_stringInput = value;
UpdateOutput();
}
}

public string StringOutput
{
get { return _stringOutput; }
set
{
_stringOutput = value;
OnPropertyChanged();
}
}

public RegexSplitterViewModel()
{
UpdateOutput();
}

public void UpdateOutput()
{
var matchesInput = RegexMatchInputs;

try
{
var regexOptions = GetRegexOptions();

var splitResults = Regex.Split(StringInput, UseRegex ? matchesInput : Regex.Escape(matchesInput), regexOptions, TimeSpan.FromMinutes(1));

StringOutput = string.Join("\r\n", RemoveEmpty ? splitResults.Where(txt => string.IsNullOrWhiteSpace(txt) == false) : splitResults);
}
catch (Exception)
{
StringOutput = "Error in regex.";
return;
}
}

protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

}
}

0 comments on commit fc83368

Please sign in to comment.