简体   繁体   English

C#解析RegexOptions中的正则表达式

[英]Regular expression in C# parsing RegexOptions

I've got a function that compares user-supplied regular expressions to strings and they have the ability to include flags in them, but only in the regular expression string. 我有一个将用户提供的正则表达式与字符串进行比较的函数,它们可以在其中包含标志,但只能在正则表达式字符串中包含标志。 That string could look something like (?!.*(alpha|beta|chi)).*standard.*/s and needs to take out the /s . 该字符串可能类似于(?!.*(alpha|beta|chi)).*standard.*/s ,需要取出/s I understand that I can pass though multiple RegexOptions with || 我知道我可以通过||通过多个RegexOptions but is there some object I can create to pass through any of the flags that are found? 但是我可以创建一些对象来通过找到的所有标志吗? I don't know if they will be sending in one flag or multiple and it could be any combination. 我不知道他们是否将发送一个或多个标志,并且可能是任意组合。

String flags = "";
if(Regex.IsMatch(regex, "/x*s*m*i*")){
    flags = Regex.Match(regex, "/x*s*m*i*").ToString();
    Regex.Replace(regex, "/x*s*m*i*", "");
}
if (flags.Contains('x')) {
    #add to some array/list of RegexOptions to pass as parameter
} else if (flags.Contains('i')) {
    #add to some array/list of RegexOptions to pass as parameter
} else if ((flags.Contains('s'))) {
    #add to some array/list of RegexOptions to pass as parameter
} else if (flags.Contains('m')) {
    #add to some array/list of RegexOptions to pass as parameter
}
if (Regex.IsMatch(source_box.Text, regex, /*Array/list of any number of RegexOptions*/))
{
    //Match found
}

Your question is quite unclear, so i will only answer to part of it, you seem to misuse regex at all, i advice you to once again review what you want to achieve and read more on regex. 您的问题尚不清楚,因此我仅会回答部分问题,您似乎完全滥用了正则表达式,我建议您再次查看您要实现的目标并阅读有关正则表达式的更多信息。

As for part that I'm quite sure: Did you thought about passing parameters of regex not inside regex but, for example, by checkboxes . 至于我很确定的部分:您是否考虑过将regex参数传递给regex而不是regex内部,例如,通过checkboxes传递。 Of course if placing of parameter is of no matter - I really don't know what you want to achieve. 当然,如果放置参数无关紧要-我真的不知道您要实现什么。 If each parameter can be used at most once then for sure if statement in middle of code which you've posted is not good, to include mixed set of options it should look like this 如果每个参数最多只能使用一次,那么请确保您所发布的代码中间的语句是否不好,要包含混合的选项集,它应该看起来像这样

EDIT (options in particular conditions doesn't correspond to actual regex options associated with these letters, they are random just to serve as example) 编辑 (在特定条件下的选项与与这些字母关联的实际正则表达式选项不对应,它们只是作为示例而随机出现)

            string flags = "/x/i/m";

            RegexOptions regExOpts = RegexOptions.None;

            if (flags.Contains('x')) {
                regExOpts |= RegexOptions.Multiline;
            }

            if (flags.Contains('i')) {
                regExOpts |= RegexOptions.IgnoreCase;
            }

            if (flags.Contains('s')) {
                regExOpts |= RegexOptions.Singleline;
            }

            if (flags.Contains('m')) {
                 regExOpts |= RegexOptions.ExplicitCapture;
            }

            MessageBox.Show(regExOpts.ToString());

EDIT After seeing your comment your problem is more clear, but still there is some stuff that I am not sure. 编辑看到您的评论后,您的问题就更加清楚了,但是我仍然不确定某些东西。 Anyway I've made some changes to code and it is probably what you've been looking for, Its late here I'm in bit of hurry so see the notice above code. 无论如何,我已经对代码进行了一些更改,这可能是您一直在寻找的东西,在这里晚了我很着急,请参见上面的代码注意事项。 Hope it will solve your problems 希望它能解决您的问题

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

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