copyright 1999 - 2014 by heinz prelle - hannover  - lenbachstraße 42 - www.visual-basic5.de | impressum
'Beispiel: VB .Net - RegEx - Telefonnummern - Vorwahl und Durchwahl trennen. Bereiche trennen.
'
Option Explicit On
Option Strict On

Imports System.Text.RegularExpressions

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
            Handles Button1.Click
        CutTelephoneNumberArea(Me.TelefonNumbers())
    End Sub

    Private Sub CutTelephoneNumberArea(ByVal TelefonNumbers As String)
        Dim Prefix As String = "(?<AreaCode>\(\d\d\d\d\))"
        Dim PhoneNumber As String = "(?<DirectDial>\d\d\d\ \d\d\d)"
        Dim mc As MatchCollection = Regex.Matches(TelefonNumbers, Prefix + " " + PhoneNumber)
        For Each m As Match In mc
            Me.ListBox1.Items.Add("Vorwahl:   " & m.Groups("AreaCode").ToString())
            Me.ListBox2.Items.Add("Durchwahl: " & m.Groups("DirectDial").ToString())
            For Each c As Capture In m.Captures
                Me.ListBox3.Items.Add("Telefonnummer: " & c.Value)
            Next
        Next
    End Sub

    Private Function TelefonNumbers() As String
        Return _
            "(0511) 461 525" & System.Environment.NewLine() & _
            "(0202) 578 949" & System.Environment.NewLine() & _
            "(0911) 664 886" & System.Environment.NewLine() & _
            "(0234) 213 992" & System.Environment.NewLine() & _
            "(0521) 888 984" & System.Environment.NewLine()
    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
            Handles MyBase.Load
        With Me
            .Text = "RegEx - Telefonnummern - Bereiche trennen"
            .Button1.Text = "Test..."
            .CenterToScreen()
        End With
    End Sub
End Class