Seazoux
Seazoux

Reputation: 631

Regex to allow only dots, letters and dashes in VB.NET

I have that code, and it only dimiss words with spaces and that it starts with +... But I have to allow only dots, letters and dashes... I think that its more simple:

Imports System.Text.RegularExpressions

Public Class Contactos

    ReadOnly pattern As String = "\s([^+\d\,]+),?"

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim cadena As String = " " & TextBox1.Text & ","

        Dim match As Match = Regex.Match(cadena, pattern)

        Do While match.Success
            frmMain.ListBox1.Items.Add(match.Groups(1).ToString)
            match = match.NextMatch()
        Loop
    End Sub
End Class

What can I do?

Thanks! :)

Upvotes: 1

Views: 2118

Answers (2)

ie.
ie.

Reputation: 6101

Please try the following pattern "(?<=(^|,\s))(?<word>[\.A-Za-z\-]+)($|,)" and use named group word to get expected values:

    Do While match.Success
        frmMain.ListBox1.Items.Add(match.Groups("word").ToString)
        match = match.NextMatch()
    Loop

Upvotes: 1

Bradley
Bradley

Reputation: 259

Try:

ReadOnly pattern As String = "\s([\.\-A-Za-z]+),?"

In the first answer the "\w" pattern will match letters, numbers and underscore. The "A-Za-z" portion I gave you will match letters only.

Upvotes: 1

Related Questions