简体   繁体   English

RegEx用于使值可选,并根据需要提供另一个值

[英]RegEx for to make a value optional and another value as required

string myQuery="RecordFiles/findrecords/$filter=userid eq 844427344589753 and
 recordid eq 55-278-278ac";

Given below is the regexpression I use to validate above query. 下面给出的是我用来验证上述查询的正则表达式。

string myRegexQueryPattern = @"^(RecordFiles/findrecords/\$filter=userid eq)\s(?<userid>\S+)((\s(and recordid eq)\s(?<recordid >\S+))|(\s*$))";

how can I correct my regEx to make my recordId as optional and userId as required ? 我如何纠正我的regEx以使我的recordId为可选,而使userId为必需?

 public static bool ValidateMyQuery(string query, Regex regex)
        {
            Match match = regex.Match(query);

            bool status = false;
            if (match.Success)
            {
                status = true;
            }
            return status;
        }

now my ValidateMyQuery returns true against myQuerystring .But if I am not sending recordId ,its returning false .I want to modify my regex so as to retun true when recordId is empty. 现在我的ValidateMyQuery对myQuerystring返回true。但是,如果我不发送recordId,则返回false。我想修改我的正则表达式,以便在recordId为空时将其调整为true。

how can I correct my regEx to make my recordId as optional and userId as required ? 我如何纠正我的regEx以使我的recordId为可选,而使userId为必需?

If you only want to assert that userid is there you can use something like: 如果您只想断言那里有用户名,则可以使用类似以下内容的方法:

string myRegexQueryPattern = @"^RecordFiles/findrecords/\$filter=.*userid eq\s\S+.*";

Since recordid is optional we don't care if it's present or not. 由于recordid是可选的,因此我们不关心它是否存在。

Your regex suggest you want to capture the values however, if that is the case I'll update my answer. 您的正则表达式建议您要捕获这些值,如果是这种情况,我将更新答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM