简体   繁体   English

正则表达式在VB.NET中仅允许点,字母和破折号

[英]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! 谢谢! :) :)

Please try the following pattern "(?<=(^|,\\s))(?<word>[\\.A-Za-z\\-]+)($|,)" and use named group word to get expected values: 请尝试以下模式"(?<=(^|,\\s))(?<word>[\\.A-Za-z\\-]+)($|,)"并使用命名的组word来获得期望的结果值:

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

Try: 尝试:

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

In the first answer the "\\w" pattern will match letters, numbers and underscore. 在第一个答案中,“ \\ w”模式将匹配字母,数字和下划线。 The "A-Za-z" portion I gave you will match letters only. 我给您的“ A-Za-z”部分仅会匹配字母。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM