无法使用正则表达式。 [英] Unable to use regex.

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

问题描述

嗨!所以我试图完成我的任务,它要求我验证一个alpanumeric,不知道怎么做,所以我看看互联网上的一些地方,发现下面粘贴的最可读的代码。但由于某种原因,它不起作用,我错过了什么?或者我做错了什么?



我尝试过:



Hi! So I was trying to do my assignment and it asks me to validate an alpanumeric, not knowing how to do it, so I look into some places around the internet and found the most readable code for me as pasted below. but for some reason it doesn't work, am I missing something? Or did I do something wrong?

What I have tried:

Public Class LoginForm

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        Form1.Close()
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text = "" Or TextBox2.Text = "" Then
            MsgBox("Username and password cannot be null")
        ElseIf TextBox1.Text.Length < 6 Or TextBox1.Text.Length > 16 Then
            MsgBox("Username and password length must be between 6 and 16")
        ElseIf TextBox2.Text.Length < 6 Or TextBox2.Text.Length > 16 Then
            MsgBox("Username and password length must be between 6 and 16")
        ElseIf IsAlphaNum(TextBox1.Text) = False Then
            MsgBox("Username must be alphanumeric")
        End If
    End Sub

    Private Function IsAlphaNum(ByVal strInputText As String) As Boolean
        Dim IsAlpha As Boolean = False
        If System.Text.RegularExpressions.Regex.IsMatch(strInputText, "^[a-zA-Z0-9]+$") Then
            IsAlpha = True
        Else
            IsAlpha = False
        End If
        Return IsAlpha
    End Function
End Class

推荐答案

)然后
IsAlpha = True
其他
IsAlpha = False
结束如果
返回IsAlpha
结束函数
结束类
") Then IsAlpha = True Else IsAlpha = False End If Return IsAlpha End Function End Class


好吧,正则表达式应该可行。但是......你的代码并不关心如果它做了什么或不做什么会发生什么如果失败并继续,你所做的就是设置一个消息框。

可能你需要在点击处理程序中添加一个Else子句,以便在模式匹配时执行特定的操作。但是我们不知道你的代码应该如何工作,所以我不能说什么。



我建议如果你有很多收获使用调试器来完全按照正在发生的事情 - 我们不能为你做到这一点!

所以,它将取决于你。

设置一个断点函数的第一行,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。
看看你能找到什么。
Well, the regex should work. But ... your code doesn't care what happens if it does or doesn't work in that all you do is put up a message box if it fails and continue.
Probably, you need to add an Else clause to the click handler to do something specific when the pattern does match. But we don't know how your code is supposed to work, so I can't say what.

I'd suggest that you would gain a lot if you used the debugger to follow exactly what is going on - we can't do that for you!
So, its going to be up to you.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.
See what you can find out.


您的代码可以简化

Your code can be simplified
Private Function IsAlphaNum(ByVal strInputText As String) As Boolean
    Dim IsAlpha As Boolean = False
    If System.Text.RegularExpressions.Regex.IsMatch(strInputText, "^[a-zA-Z0-9]+


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

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