简体   繁体   中英

Add Remove RegEx Options in .NET

I want to take regex options as input from user and I am using check boxes to take input, which looks something like following:

[ ]IngoreCase  
[ ]Multiline  
[ ]Sigleline  
[ ]RightToLeft  

etc.

My questions is that how I can pass parameters (RegexOptions) according to option(s) selected by user.

Thanks,
Amit

use Enum.GetNames

string[] names = Enum.GetNames(typeof(RegexOptions));

and from string back to enum

var option = (RegexOptions)Enum.Parse(typeof(RegexOptions), "IgnoreCase");

When you use those options, you "OR" them together into a single value. There is no need to do that in 1 statement within the regex call, you could use something like:

var options = RegexOptions.None;
if (checkBoxIgnoreCase.Checked) options = options | RegexOptions.IgnoreCase;
if (checkBoxMultiLine.Checked) options = options | RegexOptions.MultiLine;
// etc.

and use that options value in your Regex.Match .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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