0

如何检查以字符串或数字开头的特定值。在这里我附上了我的代码。我收到错误,喜欢预期的标识符。

code
----
 Dim i As String
 dim ReturnValue  as boolean
    i = 400087
    Dim s_str As String = i.Substring(0, 1)

   Dim regex As Regex = New Regex([(a - z)(A-Z)])
    ReturnValue = Regex.IsMatch(s_str, Regex)




error 

regx is type and cant be used as an expression
4

4 回答 4

3

你的变量是regex,Regex是变量的类型。

所以它是:

ReturnValue = Regex.IsMatch(s_str, regex)

但是您的正则表达式也有缺陷。正在创建一个与字符、范围和空格[(a - z)(A-Z)]完全匹配的字符类,仅此而已。()-azA-Z

在我看来,好像你想匹配字母。为此,只需使用\p{L}Unicode 属性,该属性将匹配任何语言中的字母字符。

Dim regex As Regex = New Regex("[\p{L}\d]")
于 2012-11-19T09:43:26.807 回答
2

检查字符串是否以字母数字开头

ReturnValue = Regex.IsMatch(s_str,"^[a-zA-Z0-9]+")

正则表达式解释:

^           # Matches start of string
[a-zA-Z0-9] # Followed by any letter or number
+           # at least one letter of number

在这里查看它的实际应用。

于 2012-11-19T09:44:42.187 回答
2

也许你的意思是

Dim _regex As Regex = New Regex("[(a-z)(A-Z)]")
于 2012-11-19T09:43:45.807 回答
2
Dim regex As Regex = New Regex([(a - z)(A-Z)])
ReturnValue = Regex.IsMatch(s_str, Regex)

注意大小写差异,使用regex.IsMatch. 您还需要引用正则表达式字符串:"[(a - z)(A-Z)]"


最后,该正则表达式没有意义,您匹配字符串中任何位置的任何字母或左/右括号。

要在字符串的开头进行匹配,您需要包含 start anchor ^,例如:^[a-zA-Z]匹配字符串开头的任何 ASCII 字母。

于 2012-11-19T09:44:01.043 回答