Regex 带有4个自定义参数的命令,如何解析并知道它是否';这是正确的命令吗?
更具体地说,我是在一个控制台应用程序中这样做的。我有一些类似“help”的命令,如下所示:Regex 带有4个自定义参数的命令,如何解析并知道它是否';这是正确的命令吗?,regex,vb.net,parsing,console-application,Regex,Vb.net,Parsing,Console Application,更具体地说,我是在一个控制台应用程序中这样做的。我有一些类似“help”的命令,如下所示: Select Case cmd 'cmd is readline Case "help" Console.WriteLine("This is the help command") End Select 显示我处理命令的方式。现在让我们假设我想添加一个名为“example arg1 arg2 arg3 arg4”的命令,该命令在执行时将按照用户指定的顺序输出参数,我将如何执
Select Case cmd 'cmd is readline
Case "help"
Console.WriteLine("This is the help command")
End Select
显示我处理命令的方式。现在让我们假设我想添加一个名为“example arg1 arg2 arg3 arg4”的命令,该命令在执行时将按照用户指定的顺序输出参数,我将如何执行?我想我在某个地方读过一些可以解决我的问题的东西,但我记不起它在哪里或是什么地方,我试着使用Regex.Match()来试着像这样读取命令:
If cmd.StartsWith("example") Then
Dim regex As Regex = New Regex("example (.*?) (.*?) (.*?) (.*?)")
Dim match As Match = regex.Match(cmd)
If match.Success Then
Console.WriteLine("it worked")
Else
Console.WriteLine("didnt work")
End If
End If
我之所以认为这会起作用,是因为我过去在解析WebClient数据时一直在这样做:
For Each match As Match In New Regex("this (.*?) text", RegexOptions.Singleline Or RegexOptions.IgnoreCase).Matches(source)
'the above regex says "this is text" and the match would output "is"
Console.WriteLine(match.Groups(1).Value)
Next
我只是不知道如何处理这个问题:c
Console.WriteLine("example " & msg.Split(" ")(1) & " " & msg.Split(" ")(2) & " " & msg.Split(" ")(3) & " " & msg.Split(" ")(4))
现在明白了,一开始我不明白索引是如何工作的,但它很简单,比如1=first和2=second等等,我觉得你可以在空间上拆分字符串很愚蠢。结果将是“命令”和参数的字符串数组。。。用一根管子,在上面分开。最后你需要一种分割数据的方法,这取决于你。@Proputix我以前因为某种原因无法让它工作,一天的新开始让我头脑清醒了一点,它现在工作了x3我刚刚做了
Console.WriteLine(“示例”&msg.split(“”)(1)&“&msg.split(“”)(2)&&msg.split(“”)(3)&&msg.split(“”)(4))
在一次尝试中,最终做了我想做的事情。谢谢你提醒我这么简单的事情存在,哈哈。