正则表达式在 vb6 中的用法

发布于 11-25 12:50 字数 482 浏览 0 评论 0 原文

我需要验证一个字符串,它可能包含字母数字以及特殊字符,因为我必须传递仅包含 Alpha 字符的字符串(不允许数字或任何其他特殊字符)

在当前方法中,我使用 ASCII 数字来评估每个字符是否为 alpha。是否有其他有效的方法来发现字符串中是否存在特殊字符或数字?就像我们不能使用 Like 或其他东西来检查一次而不是一个字符一个字符地检查吗?

For y = 2 To Len(sString)
    If Not ((Asc(Mid$((sString,y,1))>64 AND Asc(Mid$((sString,y,1))<91) OR _
    (Asc(Mid$((sString,y,1))>96 AND Asc(Mid$((sString,y,1))<123)) Then
        //Display an error msg
        Exit For
    End If
Next y

I need to validate a string, which might contain alphanumeric as well as special character, where as I have to pass the one which has only Alpha chars (no numbers or any other special characters are allowed)

In a current method I use ASCII numbers to evaluate each character if its alpha or not. Is there any other efficient way to discover the presence of special characters or numbers in the string? Like can't we use Like or something to check once than going character by character?

For y = 2 To Len(sString)
    If Not ((Asc(Mid$((sString,y,1))>64 AND Asc(Mid$((sString,y,1))<91) OR _
    (Asc(Mid$((sString,y,1))>96 AND Asc(Mid$((sString,y,1))<123)) Then
        //Display an error msg
        Exit For
    End If
Next y

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

森林很绿却致人迷途 2024-12-02 12:50:48

VBA 有一个原生的 Like 运算符:它的语法是非标准的,例如它的多字符通配符是 * 而 NOT 运算符是 !

If sString Like "*[!A-Za-z]*" Then
  ' Display an error msg
End If

VBA has a native Like operator: its syntax is non-standard e.g. its multi-character wildcard is * and the NOT operator is !:

If sString Like "*[!A-Za-z]*" Then
  ' Display an error msg
End If
颜漓半夏 2024-12-02 12:50:48

您必须添加对 Microsoft VBScript Regular Expressions 5.5

代码的引用来检查非字母字符:

'Prepare a regular expression object
Dim myRegExp As RegExp
Dim myMatches As MatchCollection
Dim myMatch As Match
Dim matchesFound As Boolean

Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Global = True
myRegExp.Pattern = "[^A-Za-z]+"
Set myMatches = myRegExp.Execute("abc123def#$%")
matchesFound = myMatches.Count > 0

查看 "="">如何在 Microsoft Visual Basic 6.0 中使用正则表达式,请访问 Microsoft 支持以获取更多信息。

You'll have to add a reference to Microsoft VBScript Regular Expressions 5.5

Code to check for non-alpha characters:

'Prepare a regular expression object
Dim myRegExp As RegExp
Dim myMatches As MatchCollection
Dim myMatch As Match
Dim matchesFound As Boolean

Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Global = True
myRegExp.Pattern = "[^A-Za-z]+"
Set myMatches = myRegExp.Execute("abc123def#$%")
matchesFound = myMatches.Count > 0

Check out How To Use Regular Expressions in Microsoft Visual Basic 6.0 at Microsoft Support for more info.

-黛色若梦 2024-12-02 12:50:47

您可以在 VB6 中使用正则表达式。您必须将对“Microsoft VBScript Regular Expressions 5.5”库的引用添加到您的项目中。然后您可以使用以下内容:

Dim rex As RegExp
Set rex = New RegExp
rex.Pattern = "[^a-zA-Z]"
If rex.Test(s) Then
    ' Display error message
End If

当我最初回答这个问题时,它被标记为 VB.NET;供将来参考,我原来的基于.Net的答案保留在下面

正如你所想的,这可以使用正则表达式来完成(不要忘记导入System.Text.RegularExpressions

If Regex.IsMatch(s, "[^a-zA-Z]") Then
    ' Display error msg
End If

另外,原始代码读起来像 VB6 代码,而不是 VB.NET 以下是编写原始非正则表达式代码的更易读的方法:

For Each ch As Char In someString
    If Not (ch >= "a"c AndAlso ch <= "z"c OrElse ch >= "A"c AndAlso ch <= "Z"c) Then
        ' Display error msg
        Exit For
    End If
Next

You can use regular expressions in VB6. You have to add a reference to the "Microsoft VBScript Regular Expressions 5.5" library to your project. You can then use the following:

Dim rex As RegExp
Set rex = New RegExp
rex.Pattern = "[^a-zA-Z]"
If rex.Test(s) Then
    ' Display error message
End If

When I originally answered this question, it was tagged as VB.NET; for future reference, my original .Net-based answer is retained below

As you thought, this can be done with regular expressions (don't forget Imports System.Text.RegularExpressions:

If Regex.IsMatch(s, "[^a-zA-Z]") Then
    ' Display error msg
End If

Also, the original code reads like VB6 code, not VB.NET. Here is a much more readable way to write the original non-regex code:

For Each ch As Char In someString
    If Not (ch >= "a"c AndAlso ch <= "z"c OrElse ch >= "A"c AndAlso ch <= "Z"c) Then
        ' Display error msg
        Exit For
    End If
Next
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文