使用正则表达式将字符串的前 3 个字母验证为大写

Validating a string's first 3 letters as uppercase with regex

我有一个关于 Classic ASP 的问题,关于使用正则表达式验证字符串的前 3 个字母为大写而后 4 个字符应为数字形式。

例如:

dim myString = "abc1234"

如何验证它应该是 "ABC1234" 而不是 "abc1234"

为我蹩脚的英语和作为 Classic 的新手道歉 ASP。

^[A-Z]{3}.*[0-9]{4}$

解释:

  1. ^$(字符串的开头和结尾)包围所有内容,以确保匹配所有内容
  2. [A-Z] - 给你英文字母表中的所有大写字母
  3. {3} - 其中三个
  4. .* - 可选地,中间可以有一些东西(如果不能,你可以删除它)
  5. [0-9] - 任何数字
  6. {4} - 其中 4 个

@ndn 为您提供了一个很好的正则表达式模式。要在 Classic ASP 中应用它,您只需创建一个使用该模式的 RegExp 对象,然后调用 Test() 函数根据该模式测试您的字符串。

例如:

Dim re
Set re = New RegExp
re.Pattern = "^[A-Z]{3}.*[0-9]{4}$"  ' @ndn's pattern

If re.Test(myString) Then
    ' Match. First three characters are uppercase letters and last four are digits.
Else
    ' No match.
End If