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 WyrażeniaRegularne
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length > 0)            
                listView1.Items.Add(textBox1.Text);                            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string wzorzec; 

            if (textBox2.Text.Length > 0)
                wzorzec = textBox2.Text;
            else
                return;            

            foreach (ListViewItem item in listView1.Items)
            {
                if (!Regex.IsMatch(item.Text.ToString(), wzorzec))
                    item.BackColor = Color.Red;
                else
                    item.BackColor = Color.White;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string wzorzec = @"\A\d{2}-\d{3}\s{1}",
                nazwyMiast = "";
            
            foreach(ListViewItem item in listView1.Items)
            {
                string[] rozdzielone = Regex.Split(item.Text, wzorzec);
                if(rozdzielone.Length == 2)
                    nazwyMiast += rozdzielone[1] + "\n";
            }

            MessageBox.Show(nazwyMiast);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string wzorzec = @"\A\d{2}-\d{3}\s{1}";

            foreach (ListViewItem item in listView1.Items)            
                item.Text = Regex.Replace(item.Text, wzorzec, String.Empty);            
        }
    }
}
