끝나지 않는 프로그래밍 일기


소스가 많이 더럽습니다.

RegexExpression.7z

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace RegexExpression
{
    public partial class Form1 : Form
    {
        MatchCollection colMatches;
        int numSel, nowCount, matchesCount;

        public Form1()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == string.Empty) return;
            RegexOptions RegexOption = new RegexOptions();
            
            if (checkBox1.Checked) RegexOption |= RegexOptions.IgnoreCase;
            if (checkBox2.Checked) RegexOption |= RegexOptions.Multiline;

            Regex objRegExp = new Regex(textBox1.Text, RegexOption | RegexOptions.Compiled);
            
            if (objRegExp.IsMatch(textBox3.Text)) {
                colMatches = objRegExp.Matches(textBox3.Text);

                label5.Text = "(1 / " + colMatches.Count + ")";
                if (textBox2.Text == string.Empty)
                {
                    textBox3.Focus();
                    textBox3.Select(textBox3.Text.IndexOf(colMatches[0].Value), colMatches[0].Value.Length);
                    numSel = textBox3.SelectionStart;
                    matchesCount = colMatches.Count; nowCount = 1;
                    if (colMatches.Count > 1) button2.Enabled = true;
                    else button2.Enabled = false;
                }
                else { 
                    textBox3.Text = objRegExp.Replace(textBox3.Text, textBox2.Text); 
                    button1.Enabled = false; button2.Enabled = false;
                    label5.Text = "(0 / 0)";
                }
            } else {
                MessageBox.Show("일치하는 텍스트가 없습니다.", "정규 표현식 테스터", MessageBoxButtons.OK, MessageBoxIcon.Error);
                button1.Enabled = button2.Enabled = false;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (button1.Enabled == false) button1.Enabled = true;
            if (matchesCount == nowCount + 1) button2.Enabled = false;

            textBox3.Focus();
            textBox3.SelectionStart = textBox3.Text.IndexOf(colMatches[nowCount].Value, numSel + 2);
            textBox3.SelectionLength = colMatches[nowCount].Value.Length;
            numSel = textBox3.SelectionStart;

            nowCount += 1;
            label5.Text = "(" + nowCount + " / " + matchesCount + ")";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (button2.Enabled == false) button2.Enabled = true;
            if (nowCount - 1 == 1) button1.Enabled = false;

            textBox3.Focus();
            textBox3.SelectionStart = textBox3.Text.LastIndexOf(colMatches[nowCount - 2].Value, numSel - 2);
            textBox3.SelectionLength = colMatches[nowCount - 2].Value.Length;
            numSel = textBox3.SelectionStart;

            nowCount -= 1;
            label5.Text = "(" + nowCount + " / " + matchesCount + ")";
        }
    }
}