REGEX以任何顺序验证任意数量的选项 [英] REGEX validate any number of options in any order

查看:48
本文介绍了REGEX以任何顺序验证任意数量的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用正则表达式来验证带有可能以任何顺序出现的可选参数的命令,例如:如果func有6个可能的参数,名为arg1 - arg6



func arg4 arg6 arg2或只是func应该有效,而func arg1 arg7 arg2则不是。



我不能使用split,因为它可能一个或多个参数实际上包含空格。



干杯

解决方案

可能,但是不理想。我想看更多的例子和/或铁路图。



这是我刚刚尝试过的:(?in)(( ? 'cmd'process)|(?' 的情况下 '((上部)|(低级))的情况下)|?(' rem'remove\s + \S +)|('unknown'\S +)){1? }





我最喜欢的RegEx页面: https://msdn.microsoft.com/en-us/library/az24scfc(v = vs.110)的.aspx [ ^ ]



我希望您尝试过Expresso: http:// www .ultrapico.com / Expresso.htm [ ^ ]



这里是我的RegexTester链接: RegexTester [ ^ ]











我会在我的命令行解析器中输入一个链接:ParsedCommandLine [ ^ ]


我强烈建议不要在解析/评估命令行参数时使用Regex。对于Regex来说,这不是一个非常合适的任务,解决方案会过于复杂而且几乎无法使用(你验证了它,现在是什么?),而且重要的是,它不是真正可维护而且不灵活。或者,我可以建议我的实用程序,或者我在文章中引用的另一个实用程序:枚举 - 基于命令行实用程序 [ ^ ]。



-SA


我想出了什么似乎是一个简单,可扩展的解决方案。我只是创建一个更简单的正则表达式列表,每个可能的参数一个。然后我遍历该列表,如果我得到一个特定参数的匹配,我从目标字符串中删除匹配。这可以确保每个参数只在命令字符串中一次,并且它在字符串中的位置无关紧要(任何顺序 - 排序)。最后,如果命令字符串中还有任何内容,则它的参数无效。



因此,对于上述评论中提到的简单情况,我会创建一个列表:

 List< string> list =  new 列表< string>(){
@

Can I use a regex to validate a command with optional arguments that may appear in any order eg: if an func has 6 possible arguments named arg1 - arg6

"func arg4 arg6 arg2" or just "func" should be valid, while "func arg1 arg7 arg2" is not.

I can't use split, as it is possible that one or more of the arguments actually contain spaces.

Cheers

解决方案

Possible, but not ideal. I'd want to see more examples and/or a railroad diagram.

Here's what I just experimented with: (?in)((?'cmd'process)|(?'case'((upper)|(lower))case)|(?'rem'remove\s+\S+)|(?'unknown'\S+)){1}


My favorite RegEx page: https://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx[^]

I hope you've tried Expresso: http://www.ultrapico.com/Expresso.htm[^]

And here's a link to my RegexTester: RegexTester[^]





And I'll throw in a link to my command line parser while I'm at it: ParsedCommandLine[^]


I would strongly advise not to use Regex in parsing/evaluation of command line arguments. This is not exactly very suitable task for Regex, the solution will be overly complicated and almost unusable (you validated it, now what?) and, importantly, not really maintainable and not flexible. Alternatively, I can suggest my utility, or also another one I referenced in my article: Enumeration-based Command Line Utility[^].

—SA


I've come up with what seems to be a simple, and extensible solution. I just create a list of much simpler regular expressions, one for each of the possible arguments. I then iterate through that list, and if I get a match for a particular argument, I remove the match from the target string. This ensures that each argument is only in the command string once, and it doesn't matter where in the string it is found (any order - sorted). At the end, if there is anything left in the command string, then it had an invalid argument.

So , for the simple case as mentioned in the comments above, I would create a list with:

List<string> list = new List<string>() {
    @"


这篇关于REGEX以任何顺序验证任意数量的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆