www.otizm.platformu.org

14 Mart 2009 Cumartesi

CSharpta Text Dosyalarından Regular Expressions ile Kayıt Okumak

Merhabalar ... İlk makalem olacağı için heyecan var tabiki de.... Bügün ben (Regex ile bu ara kafa yorduğum için) yazmış olduğum küçük bir regex uygulamasını sizlerle paylaşmak istiyorum.Uygulama bize text tabanlı dosya içerisinde bulunan düzensiz yazılmış 11 haneli bütün Tc kimlik numaralarını listbox yardımıyla görüntüleyecek

Öncelikle projemizi form uygulaması olarak açıyoruz. Daha sonra elde edilen form üzerine bir buton ve tc kimlik noları goruntülemesine yardımcı olacak bir listbox ekliyoruz.



Daha sonra kodumuzu Button_click eventinin altına yazmaya başlıyoruz...Text tabanlı dosya okuma yapılacağı ve Regex sınıfını kullanacağımız için system.IO ve System.Text.RegularExpressions namespacesini yazmayı unutmuyoruz


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.IO;
using System.Text.RegularExpressions;

namespace regexdeneme
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

string yol = @"C:\Documents and Settings\Sehmus\Desktop\project\a.txt";

FileStream Fp;
StreamReader sr;

Fp = new FileStream(yol , FileMode.Open);

sr = new StreamReader(Fp);
string deneme = sr.ReadToEnd();

Regex benimregex = new Regex("[0-9]{1,11}");
MatchCollection mc = Regex.Matches(deneme, "[0-9]{1,11}");
foreach (Match bulunan in mc)
{
if (bulunan.Length > 10)

listBox1.Items.Add(bulunan);
}
sr.Close();

}


}
}

Hiç yorum yok:

Yorum Gönder