[英]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: 我想将regex选项作为用户输入,我使用复选框来获取输入,如下所示:
[ ]IngoreCase
[ ]Multiline
[ ]Sigleline
[ ]RightToLeft
etc. 等等
My questions is that how I can pass parameters (RegexOptions) according to option(s) selected by user. 我的问题是我如何根据用户选择的选项传递参数(RegexOptions)。
Thanks, 谢谢,
Amit 阿米特
use Enum.GetNames 使用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:
在正则表达式调用中的1语句中没有必要这样做,你可以使用类似的东西:
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
. 并在
Regex.Match
使用该options
值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.