regex and excel

13
Regex and Excel in VB.NET

Upload: nela-rosa-harianja

Post on 18-Feb-2016

228 views

Category:

Documents


4 download

DESCRIPTION

kk

TRANSCRIPT

Page 1: Regex and Excel

Regex and Excel in VB.NET

Page 2: Regex and Excel

introduction

• Sebuah ekspresi reguler adalah pola yang dapat dicocokkan dengan

sebuah input teks. Net framework menyediakan mesin ekspresi reguler yang memungkinkan pencocokan tersebut. Pola terdiri dari satu atau lebih karakter literal, operator, atau konstruksi. Ekspresi reguler menyediakan metode yang kuat, fleksibel, dan efisien untuk pengolahan teks. Notasi pencocokan pola ekstensif ekspresi reguler memungkinkan Anda dengan cepat mengurai sejumlah besar teks untuk menemukan pola karakter tertentu; untuk memvalidasi teks untuk memastikan bahwa itu cocok dengan pola yang telah ditentukan (seperti alamat e-mail); untuk mengekstrak, mengedit, mengganti, atau menghapus substring teks; dan untuk menambahkan string

Page 3: Regex and Excel

How Regular Expressions Work

• Inti dari pengolahan teks dengan ekspresi reguler adalah mesin ekspresi reguler, yang diwakili oleh objek System.Text.RegularExpressions.Regex di Framework. Minimal, pengolahan teks menggunakan ekspresi reguler mengharuskan mesin ekspresi reguler disediakan dengan dua item berikut informasi: Pola ekspresi reguler untuk mengidentifikasi dalam teks. Dalam .NET Framework, pola ekspresi reguler didefinisikan oleh sintaks khusus atau bahasa, yang kompatibel dengan Perl 5 ekspresi reguler dan menambahkan beberapa fitur tambahan seperti pencocokan kanan-ke-kiri. Teks untuk mengurai untuk pola ekspresi reguler.

Page 4: Regex and Excel

Membangun untuk Mendefinisikan Regular Expressions

• Karakter lolos: \ t, \ n, \ r Kelas karakter: [character_group], [pertama - terakhir] Jangkar: ^ (Pertandingan harus mulai dari awal string atau baris.), $ (Pertandingan harus terjadi pada akhir string atau sebelum \ n pada akhir garis atau tali.) Pengelompokan Constructs: (subexpression) -Captures subexpression cocok Bilangan: * - Cocok elemen sebelumnya nol atau lebih kali Backreference Constructs: memungkinkan sub-ekspresi yang cocok sebelumnya diidentifikasi kemudian dalam ekspresi reguler yang sama. Alternatif Constructs: | Substitusi: $ number (Pengganti substring cocok dengan nomor kelompok)

Page 5: Regex and Excel

The regex class

• Fungsi Umum IsMatch (input As String) As Boolean

Menunjukkan apakah ekspresi reguler yang ditentukan dalam konstruktor Regex menemukan kecocokan dalam input string yang ditentukan. Fungsi Umum IsMatch (input As String, startat As Integer) As Boolean Menunjukkan apakah ekspresi reguler yang ditentukan dalam konstruktor Regex menemukan kecocokan dalam input string yang ditentukan, mulai dari posisi awal yang ditentukan dalam string. Masyarakat bersama Fungsi IsMatch (input As String, pola As String) As Boolean Menunjukkan apakah ekspresi reguler yang ditentukan menemukan kecocokan dalam input string yang ditentukan. Fungsi Umum Pertandingan (input As String) As MatchCollection Mencari masukan string yang ditentukan untuk semua kejadian dari ekspresi reguler. Fungsi Umum Ganti (input As String, penggantian As String) As String Dalam input string yang ditentukan, menggantikan semua string yang cocok dengan pola ekspresi reguler dengan string pengganti yang ditentukan. Fungsi Umum Berpisah (input As String) As String () Membagi string masukan ke dalam array dari substring di posisi yang didefinisikan oleh pola ekspresi reguler yang ditentukan dalam konstruktor Regex.

Page 6: Regex and Excel

Contoh 1

Imports System.Text.RegularExpressions Module Example Public Sub Main() Dim pattern As Stringa = "(Mr\.? |Mrs\.? |Miss |Ms\.? )" Dim names() As String = { "Mr. Henry Hunt", "Ms. Sara Samuels", _ "Abraham Adams", "Ms. Nicole Norris" } For Each name As String In names Console.WriteLine(Regex.Replace(name, pattern, String.Empty)) Next End Sub End Module ' The example displays the following output: ' Henry Hunt ' Sara Samuels ' Abraham Adams ' Nicole Norris

Page 7: Regex and Excel

Contoh 1

Imports System.Text.RegularExpressions Module regexProg Sub showMatch(ByVal text As String, ByVal expr As String) Console.WriteLine("The Expression: " + expr) Dim mc As MatchCollection = Regex.Matches(text, expr) Dim m As Match For Each m In mc Console.WriteLine(m) Next m End Sub Sub Main() Dim str As String = "A Thousand Splendid Suns" Console.WriteLine("Matching words that start with 'S': ") showMatch(str, "\bS\S*") Console.ReadKey() End Sub End Module

Page 8: Regex and Excel

Contoh 2

Imports System.Text.RegularExpressions Module regexProg Sub showMatch(ByVal text As String, ByVal expr As String) Console.WriteLine("The Expression: " + expr) Dim mc As MatchCollection = Regex.Matches(text, expr) Dim m As Match For Each m In mc Console.WriteLine(m) Next m End Sub Sub Main() Dim str As String = "make a maze and manage to measure it" Console.WriteLine("Matching words that start with 'm' and ends with 'e': ") showMatch(str, "\bm\S*e\b") Console.ReadKey() End Sub End Module

Page 9: Regex and Excel

Excel sheet

• VB.Net menyediakan dukungan untuk interoperabilitas antara model objek COM dari Microsoft Excel dan aplikasi Anda. Untuk memanfaatkan interoperabilitas ini dalam aplikasi Anda, Anda perlu mengimpor namespace Microsoft.Office.Interop.Excel dalam Formulir Aplikasi Windows Anda.

Page 10: Regex and Excel

Creating an Excel Application from VB.Net

• Mari kita mulai dengan menciptakan aplikasi

Jendela Formulir dengan mengikuti langkah-langkah berikut di Microsoft Visual Studio: File -> New Project -> Windows Forms Aplikasi Akhirnya, pilih OK, Microsoft Visual Studio menciptakan proyek Anda dan menampilkan berikut Form1. Masukkan Button1 Tombol kontrol dalam bentuk. Menambahkan referensi ke Perpustakaan objek Microsoft Excel untuk proyek Anda.

Page 11: Regex and Excel

Select Add Reference from the Project Menu.

Page 12: Regex and Excel

On the COM tab, locate Microsoft Excel Object Library and then click

Select.

Page 13: Regex and Excel

Imports Excel = Microsoft.Office.Interop.Excel Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim appXL As Excel.Application Dim wbXl As Excel.Workbook Dim shXL As Excel.Worksheet Dim raXL As Excel.Range ' Start Excel and get Application object. appXL = CreateObject("Excel.Application") appXL.Visible = True ' Add a new workbook. wbXl = appXL.Workbooks.Add shXL = wbXl.ActiveSheet ' Add table headers going cell by cell. shXL.Cells(1, 1).Value = "First Name" shXL.Cells(1, 2).Value = "Last Name" shXL.Cells(1, 3).Value = "Full Name" shXL.Cells(1, 4).Value = "Specialization" ' Format A1:D1 as bold, vertical alignment = center. With shXL.Range("A1", "D1") .Font.Bold = True .VerticalAlignment = Excel.XlVAlign.xlVAlignCenter End With ' Create an array to set multiple values at once. Dim students(5, 2) As String students(0, 0) = "Zara" students(0, 1) = "Ali" students(1, 0) = "Nuha" students(1, 1) = "Ali" students(2, 0) = "Arilia" students(2, 1) = "RamKumar" students(3, 0) = "Rita" students(3, 1) = "Jones" students(4, 0) = "Umme" students(4, 1) = "Ayman"

' Fill A2:B6 with an array of values (First and Last Names). shXL.Range("A2", "B6").Value = students ' Fill C2:C6 with a relative formula (=A2 & " " & B2). raXL = shXL.Range("C2", "C6") raXL.Formula = "=A2 & "" "" & B2" ' Fill D2:D6 values. With shXL .Cells(2, 4).Value = "Biology" .Cells(3, 4).Value = "Mathmematics" .Cells(4, 4).Value = "Physics" .Cells(5, 4).Value = "Mathmematics" .Cells(6, 4).Value = "Arabic" End With ' AutoFit columns A:D. raXL = shXL.Range("A1", "D1") raXL.EntireColumn.AutoFit() ' Make sure Excel is visible and give the user control ' of Excel's lifetime. appXL.Visible = True appXL.UserControl = True ' Release object references. raXL = Nothing shXL = Nothing wbXl = Nothing appXL.Quit() appXL = Nothing Exit Sub Err_Handler: MsgBox(Err.Description, vbCritical, "Error: " & Err.Number) End Sub End Class