Text Helper

namespace TextHelper
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.txtSource = new System.Windows.Forms.TextBox();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.txtFind = new System.Windows.Forms.Button();
            this.txtFindNext = new System.Windows.Forms.Button();
            this.chkRowFilter = new System.Windows.Forms.CheckBox();
            this.txtRegPattern = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.btnUndo = new System.Windows.Forms.Button();
            this.btnGo = new System.Windows.Forms.Button();
            this.txtReplacement = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.txtSuffix = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.txtPrefix = new System.Windows.Forms.TextBox();
            this.label4 = new System.Windows.Forms.Label();
            this.btnLineTrim = new System.Windows.Forms.Button();
            this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.SuspendLayout();
            // 
            // txtSource
            // 
            this.txtSource.AcceptsReturn = true;
            this.txtSource.AcceptsTab = true;
            this.txtSource.Location = new System.Drawing.Point(12, 12);
            this.txtSource.Multiline = true;
            this.txtSource.Name = "txtSource";
            this.txtSource.ScrollBars = System.Windows.Forms.ScrollBars.Both;
            this.txtSource.Size = new System.Drawing.Size(934, 424);
            this.txtSource.TabIndex = 0;
            this.txtSource.WordWrap = false;
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.txtFind);
            this.groupBox1.Controls.Add(this.txtFindNext);
            this.groupBox1.Controls.Add(this.chkRowFilter);
            this.groupBox1.Controls.Add(this.txtRegPattern);
            this.groupBox1.Controls.Add(this.label1);
            this.groupBox1.Location = new System.Drawing.Point(12, 442);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(934, 40);
            this.groupBox1.TabIndex = 2;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Filter";
            // 
            // txtFind
            // 
            this.txtFind.Location = new System.Drawing.Point(730, 10);
            this.txtFind.Name = "txtFind";
            this.txtFind.Size = new System.Drawing.Size(91, 24);
            this.txtFind.TabIndex = 4;
            this.txtFind.Text = "Find";
            this.txtFind.UseVisualStyleBackColor = true;
            this.txtFind.Click += new System.EventHandler(this.txtFind_Click);
            // 
            // txtFindNext
            // 
            this.txtFindNext.Enabled = false;
            this.txtFindNext.Location = new System.Drawing.Point(837, 10);
            this.txtFindNext.Name = "txtFindNext";
            this.txtFindNext.Size = new System.Drawing.Size(91, 24);
            this.txtFindNext.TabIndex = 3;
            this.txtFindNext.Text = "Find Next";
            this.txtFindNext.UseVisualStyleBackColor = true;
            this.txtFindNext.Click += new System.EventHandler(this.txtFindNext_Click);
            // 
            // chkRowFilter
            // 
            this.chkRowFilter.AutoSize = true;
            this.chkRowFilter.Location = new System.Drawing.Point(646, 15);
            this.chkRowFilter.Name = "chkRowFilter";
            this.chkRowFilter.Size = new System.Drawing.Size(70, 17);
            this.chkRowFilter.TabIndex = 2;
            this.chkRowFilter.Text = "RowFilter";
            this.chkRowFilter.UseVisualStyleBackColor = true;
            // 
            // txtRegPattern
            // 
            this.txtRegPattern.Location = new System.Drawing.Point(90, 13);
            this.txtRegPattern.Name = "txtRegPattern";
            this.txtRegPattern.Size = new System.Drawing.Size(550, 20);
            this.txtRegPattern.TabIndex = 1;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(6, 16);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(78, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Regex Pattern:";
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.txtReplacement);
            this.groupBox2.Controls.Add(this.label2);
            this.groupBox2.Controls.Add(this.txtSuffix);
            this.groupBox2.Controls.Add(this.label3);
            this.groupBox2.Controls.Add(this.txtPrefix);
            this.groupBox2.Controls.Add(this.label4);
            this.groupBox2.Location = new System.Drawing.Point(12, 488);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(934, 76);
            this.groupBox2.TabIndex = 2;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Process Text";
            // 
            // btnUndo
            // 
            this.btnUndo.Enabled = false;
            this.btnUndo.Location = new System.Drawing.Point(637, 570);
            this.btnUndo.Name = "btnUndo";
            this.btnUndo.Size = new System.Drawing.Size(91, 24);
            this.btnUndo.TabIndex = 2;
            this.btnUndo.Text = "Undo";
            this.btnUndo.UseVisualStyleBackColor = true;
            this.btnUndo.Click += new System.EventHandler(this.btnUndo_Click);
            // 
            // btnGo
            // 
            this.btnGo.Location = new System.Drawing.Point(849, 570);
            this.btnGo.Name = "btnGo";
            this.btnGo.Size = new System.Drawing.Size(91, 24);
            this.btnGo.TabIndex = 2;
            this.btnGo.Text = "Go";
            this.btnGo.UseVisualStyleBackColor = true;
            this.btnGo.Click += new System.EventHandler(this.btnGo_Click);
            // 
            // txtReplacement
            // 
            this.txtReplacement.AcceptsReturn = true;
            this.txtReplacement.AcceptsTab = true;
            this.txtReplacement.Location = new System.Drawing.Point(422, 13);
            this.txtReplacement.Multiline = true;
            this.txtReplacement.Name = "txtReplacement";
            this.txtReplacement.ScrollBars = System.Windows.Forms.ScrollBars.Both;
            this.txtReplacement.Size = new System.Drawing.Size(218, 52);
            this.txtReplacement.TabIndex = 1;
            this.txtReplacement.WordWrap = false;
            this.txtReplacement.MouseMove += new System.Windows.Forms.MouseEventHandler(this.txtReplacement_MouseMove);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(343, 16);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(73, 13);
            this.label2.TabIndex = 0;
            this.label2.Text = "Replacement:";
            // 
            // txtSuffix
            // 
            this.txtSuffix.AcceptsReturn = true;
            this.txtSuffix.AcceptsTab = true;
            this.txtSuffix.Location = new System.Drawing.Point(693, 13);
            this.txtSuffix.Multiline = true;
            this.txtSuffix.Name = "txtSuffix";
            this.txtSuffix.ScrollBars = System.Windows.Forms.ScrollBars.Both;
            this.txtSuffix.Size = new System.Drawing.Size(235, 52);
            this.txtSuffix.TabIndex = 1;
            this.txtSuffix.WordWrap = false;
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(651, 16);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(36, 13);
            this.label3.TabIndex = 0;
            this.label3.Text = "Suffix:";
            // 
            // txtPrefix
            // 
            this.txtPrefix.AcceptsReturn = true;
            this.txtPrefix.AcceptsTab = true;
            this.txtPrefix.Location = new System.Drawing.Point(90, 13);
            this.txtPrefix.Multiline = true;
            this.txtPrefix.Name = "txtPrefix";
            this.txtPrefix.ScrollBars = System.Windows.Forms.ScrollBars.Both;
            this.txtPrefix.Size = new System.Drawing.Size(239, 52);
            this.txtPrefix.TabIndex = 1;
            this.txtPrefix.WordWrap = false;
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(48, 16);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(36, 13);
            this.label4.TabIndex = 0;
            this.label4.Text = "Prefix:";
            // 
            // btnLineTrim
            // 
            this.btnLineTrim.Location = new System.Drawing.Point(742, 570);
            this.btnLineTrim.Name = "btnLineTrim";
            this.btnLineTrim.Size = new System.Drawing.Size(91, 24);
            this.btnLineTrim.TabIndex = 2;
            this.btnLineTrim.Text = "Line Trim";
            this.btnLineTrim.UseVisualStyleBackColor = true;
            this.btnLineTrim.Click += new System.EventHandler(this.btnLineTrim_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(959, 604);
            this.Controls.Add(this.btnLineTrim);
            this.Controls.Add(this.btnUndo);
            this.Controls.Add(this.groupBox2);
            this.Controls.Add(this.btnGo);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.txtSource);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.Name = "Form1";
            this.Text = "TextHelper";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox txtSource;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.TextBox txtRegPattern;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Button txtFindNext;
        private System.Windows.Forms.CheckBox chkRowFilter;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.TextBox txtSuffix;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.TextBox txtPrefix;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.TextBox txtReplacement;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button btnGo;
        private System.Windows.Forms.Button txtFind;
        private System.Windows.Forms.Button btnUndo;
        private System.Windows.Forms.Button btnLineTrim;
        private System.Windows.Forms.ToolTip toolTip1;
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace TextHelper
{
    public partial class Form1 : Form
    {
        private MatchCollection matches;
        private int matchIndex = -1;

        private Dictionary<int,KeyValuePair<int,int>> matchedRows;
        private int rowMatchIndex = -1;

        private List<string> GoList = new List<string>();

        public Form1()
        {
            InitializeComponent();
        }

        private void txtFindNext_Click(object sender, EventArgs e)
        {
            if (!chkRowFilter.Checked)
            {
                if (matches != null)
                {
                    matchIndex++;
                    if (matchIndex >= matches.Count)
                    {
                        matchIndex = matches.Count - 1;
                    }
                    var match = matches[matchIndex];
                    txtSource.SelectionStart = match.Index;
                    txtSource.SelectionLength = match.Length;
                    txtSource.Select();
                    txtSource.ScrollToCaret();
                }
            }
            else
            {
                if (matchedRows !=null)
                {
                    rowMatchIndex++;
                    if (rowMatchIndex >= matchedRows.Count)
                    {
                        rowMatchIndex = matchedRows.Count - 1;
                    }
                    var keys = matchedRows.Keys.ToArray();
                    var row = matchedRows[keys[rowMatchIndex]];
                    txtSource.SelectionStart = row.Key;
                    txtSource.SelectionLength = row.Value;
                    txtSource.Select();
                    txtSource.ScrollToCaret();
                }
            }
        }

        private void txtFind_Click(object sender, EventArgs e)
        {
            txtFindNext.Enabled = false;

            if (string.IsNullOrEmpty(txtRegPattern.Text))
            {
                return;
            }

            var source = txtSource.Text;
            if (!chkRowFilter.Checked)
            {
                matches = null;
                matchIndex = -1;

                try
                {
                    matches = Regex.Matches(source, txtRegPattern.Text);
                }
                catch (Exception ex)
                {
                    matches = null;
                    matchIndex = -1;
                    MessageBox.Show(ex.ToString());
                    return;
                }
                if (matches.Count > 0)
                {
                    matchIndex = 0;
                    var match = matches[matchIndex];
                    txtSource.SelectionStart = match.Index;
                    txtSource.SelectionLength = match.Length;
                    txtSource.Select();
                    txtSource.ScrollToCaret();

                    if (matches.Count > 1)
                    {
                        txtFindNext.Enabled = true;
                    }
                }
                else
                {
                    matches = null;
                    matchIndex = -1;
                    MessageBox.Show("Nothing is matched!");
                    return;
                }
            }
            else
            {
                matchedRows = new Dictionary<int,KeyValuePair<int,int>>();
                rowMatchIndex = -1;
                var lines = txtSource.Text.Split(new string[] { "\r\n" }, StringSplitOptions.None);
                var lastPos = 0;
                for (int i = 0; i < lines.Count(); i++)
                {
                    var line = lines[i];
                    var isMatch = false;
                    try
                    {
                        isMatch = Regex.IsMatch(line, txtRegPattern.Text, RegexOptions.None);
                    }
                    catch (Exception ex)
                    {
                        matchedRows = null;
                        rowMatchIndex = -1;
                        MessageBox.Show(ex.ToString());
                        return;
                    }
                    if (isMatch)
                    {
                        matchedRows.Add(i, new KeyValuePair<int, int>(lastPos, line.Length));
                    }
                    lastPos += line.Length + 2;
                }
                if (matchedRows.Count() > 0)
                {
                    rowMatchIndex = 0;
                    var keys = matchedRows.Keys.ToArray();
                    var row = matchedRows[keys[0]];
                    txtSource.SelectionStart = row.Key;
                    txtSource.SelectionLength = row.Value;
                    txtSource.Select();
                    txtSource.ScrollToCaret();

                    if (matchedRows.Count > 1)
                    {
                        txtFindNext.Enabled = true;
                    }
                }
                else
                {
                    matchedRows = null;
                    rowMatchIndex = -1;
                    MessageBox.Show("Nothing is matched!");
                    return;
                }
            }
        }

        private void btnUndo_Click(object sender, EventArgs e)
        {
            if (GoList.Count > 0)
            {
                if (MessageBox.Show("Undo will lost last change,do you want undo this change?", "", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
                {
                    txtSource.Text = GoList[GoList.Count - 1];
                    GoList.RemoveAt(GoList.Count - 1);
                    if (GoList.Count == 0)
                    {
                        btnUndo.Enabled = false;
                    }
                }
            }
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtRegPattern.Text))
            {
                return;
            }

            var source = txtSource.Text;
            var text = txtSource.Text ;
            var sbPrevious = new StringBuilder();
            var sbNext = new StringBuilder(text);

            if (!chkRowFilter.Checked)
            {
                while(true)
                {
                    Match match = null;
                    try
                    {
                        match = Regex.Match(sbNext.ToString(), txtRegPattern.Text, RegexOptions.None);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                        return;
                    }
                    
                    if (match != null && match.Success)
                    {
                        var format = string.IsNullOrEmpty(txtReplacement.Text) ? "{$self}" : txtReplacement.Text;
                        var replace = txtPrefix.Text + format.ToLower().Replace(@"{$self}", match.Value) + txtSuffix.Text;
                        sbNext.Remove(match.Index, match.Length);
                        sbNext.Insert(match.Index, replace);
                        sbPrevious.Append(sbNext.ToString().Substring(0, match.Index + replace.Length));
                        sbNext.Remove(0, match.Index + replace.Length);
                    }
                    else
                    {
                        sbPrevious.Append(sbNext.ToString());
                        break;
                    }
                }

                txtSource.Text = sbPrevious.ToString();

                GoList.Add(source);
                btnUndo.Enabled = true;
                
            }
            else
            {
                var lines = txtSource.Text.Split(new string[] { "\r\n" }, StringSplitOptions.None).ToList();
                for (int i = lines.Count - 1; i >= 0; i--)
                {
                    var line = lines[i];
                    Match match = null;
                    try
                    {
                        match = Regex.Match(line, txtRegPattern.Text, RegexOptions.None);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                        return;
                    }
                    if (match != null && match.Success)
                    {
                        var format = string.IsNullOrEmpty(txtReplacement.Text) ? "{$self}" : txtReplacement.Text;
                        var replace = txtPrefix.Text + format.ToLower().Replace(@"{$self}", match.Value) + txtSuffix.Text;
                        var sb = new StringBuilder(lines[i]);
                        sb.Remove(match.Index, match.Length);
                        sb.Insert(match.Index, replace);
                        lines[i] = sb.ToString();
                    }
                }

                var sb2 = new StringBuilder();
                lines.ForEach(x => sb2.Append(x).Append("\r\n"));
                if (sb2.Length >= 2)
                {
                    sb2.Remove(sb2.Length - 2, 2);
                }
                txtSource.Text = sb2.ToString();

                GoList.Add(source);
                btnUndo.Enabled = true;
            }
        }

        private void btnLineTrim_Click(object sender, EventArgs e)
        {
            var source = txtSource.Text;
            var lines = txtSource.Text.Split(new string[] { "\r\n" }, StringSplitOptions.None).ToList();
            var sb = new StringBuilder();
            lines.ForEach(x => sb.Append(x.Trim()).Append("\r\n"));
            if (sb.Length >= 2)
            {
                sb.Remove(sb.Length - 2, 2);
            }
            txtSource.Text = sb.ToString();

            GoList.Add(source);
            btnUndo.Enabled = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void txtReplacement_MouseMove(object sender, MouseEventArgs e)
        {
            toolTip1.SetToolTip(txtReplacement, "Expression \"{$self}\" can express matched value itself.");
        }
    }
}

 

posted on 2013-02-02 13:37  空明流光  阅读(213)  评论(0编辑  收藏  举报

导航