我正在尝试设计简单的编译器(扫描仪),我需要知道如何编写代码识别标识符包含字母和数字,例如id45并打印
答案 0 :(得分:0)
如果你想检查一个字符串是否以字母开头,并且之后可能包含字母或数字,你可以使用常规表达式,而不知道你想要什么。
Function IsVar(txt as string) as Boolean
'txt ="id45"
IsVar = False
Dim re1 As String
re1 ="((?:[a-z][a-z0-9_]*))"
Dim r As New RegExp
r.Pattern = re1
r.IgnoreCase = True
bX = re.Test(sSendLine)
If bX = True Then
IsVar = True
End If
End Function
基本上常规指令“说”
[a-z] match a single character present in the list
a-z a single character in the range between a and z (case sensitive)
[a-z0-9_]* match a single character present in the list
a-z a single character in the range between a and z (case sensitive)
0-9 or a single character in the range between 0 and 9
_ or the character _ (underscore)
* do the previous match from zero to unlimited times (as many times as possible) [greedy]
如果我们应该有一个字符串“id45”。
the first character is an I -- first instruction successful (next)
character 2 = d -- second instruction successful (repeat)
character 3 = 4 -- second instruction successful (repeat)
character 4 = 5 -- second instruction successful (repeat)
no more characters -- return true
如果你不想要和下划线,只需在正则表达式中取出下划线。
希望这会有所帮助 查理