如何制作有效的正则表达式 [英] How do I make a valid regex

查看:69
本文介绍了如何制作有效的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何具体说明gmail或yahoo email id

以gmail.com或yahoo.com结尾或yahoo.co.in



 公共 功能 ValidEmail( ByVal 电子邮件作为 字符串 ByRef 无效作为 字符串 ByRef  textbox  As  TextBox) As   String  


Dim 模式 As String = ([0-9a-zA-Z \ -_] * @)gmail.com ????


Dim 匹配 As System.Text.RegularExpressions.Match = Regex.Match(Email.Trim(),pattern,RegexOptions.IgnoreCase)

If (match.Success)然后
' MsgBox(有效的电子邮件ID)
返回电子邮件
否则
无效=无效&电子邮件& Environment.NewLine
' MsgBox(无效的电子邮件ID)
结束 如果
返回 Nothing

结束 功能





我的尝试:



我如何具体说明gmail或yahoo email id

以gmail.com或yahoo.com结尾或yahoo.co.in

解决方案

请检查



正则表达式 - 正则表达式 - 验证Gmail地址 - 堆栈溢出 [ ^ ]

在这种情况下,将字符串拆分为@字符会更有意义。然后验证第一部分不是空的,第二部分是支持的部分。



或者如果使用Regex类,那么我将捕获域部分和然后根据预定义列表进行检查。



阅读文档以获取更多信息:Regex Class(System.Text.RegularExpressions) [ ^ ]


试试这个:

。+ @ gmail.com |。+ @ yahoo.com |。+ @ yahoo.co.in 


how do i be specific of gmail or yahoo email id only
ends with gmail.com or yahoo.com Or yahoo.co.in

Public Function ValidEmail(ByVal Email As String, ByRef invalid As String, ByRef textbox As TextBox) As String


        Dim pattern As String = "([0-9a-zA-Z\-_]*@)gmail.com" ????


        Dim match As System.Text.RegularExpressions.Match = Regex.Match(Email.Trim(), pattern, RegexOptions.IgnoreCase)

        If (match.Success) Then
            'MsgBox("Valid email id")
            Return Email
        Else
            invalid = invalid & Email & Environment.NewLine
  'MsgBox("Invalid email id")
        End If
        Return Nothing

    End Function



What I have tried:

how do i be specific of gmail or yahoo email id only
ends with gmail.com or yahoo.com Or yahoo.co.in

解决方案

Please check

regex - Regular Expression - Validate Gmail addresses - Stack Overflow[^]


In a case like this, it would make much more sense to split the string on '@' character. Then validate that first part is not empty and that second part is one of the supported on.

Or if Regex class is used, then I would capture the domain part and then check it against predefined list.

Read the documentation for more information: Regex Class (System.Text.RegularExpressions)[^]


Try this:

.+@gmail.com|.+@yahoo.com|.+@yahoo.co.in


这篇关于如何制作有效的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆