按位或组合 [英] Bitwise OR Combination
问题描述
这是最常用的正则表达式的功能之一
This is one of the most used Regex functions
Regex.IsMatch("Test text for regex test.", "(test)",
RegexOptions.IgnoreCase | RegexOptions.Multiline);
你能解释方法,如何Regex.IsMatch的作品? 我的意思是如何处理位或RegexOptions参数? 它是如何定义方法的参数?
Can you explain how Regex.IsMatch method works ? I mean how it handles bitwise OR RegexOptions parameters ? How it defines method parameters ?
感谢您的答复!
推荐答案
RegexOptions
是一个枚举的 [国旗]
属性适用于它。这允许按位操作,以被应用到各种值。
RegexOptions
is an enumeration with the [Flags]
attribute applied to it. This allows bitwise operations to be applied to the various values.
您也可以做同样的事情:
You can also do something similar:
[Flags]
enum MyOptions {
UpperCase = 1,
Reverse = 2,
Trim = 4
}
public static void DoTransform(MyOptions options) {
if ((options & MyOptions.UpperCase) == MyOptions.UpperCase) {
/* Do Upper case transform */
}
if ((options & MyOptions.Reverse) == MyOptions.Reverse) {
/* Do Reverse transform */
}
/* etc, ... */
}
DoTransform(MyOptions.UpperCase | MyOptions.Reverse);
我刚刚做多一点的基础上坦率的评论挖掘和他是正确的使用或不使用 [国旗]
属性时,code以上的意志编译并运行。
I've just done a bit more digging based on Frank's comment and he is correct that with or without the [Flags]
attribute, the code above will compile and run.
已经有关于什么 [国旗]
属性的不的做,但除其他意见将会影响到的ToString()
结果似乎没有人知道或想解释一下它的确实的事情。在code我写的,我饰我打算为位域与 [国旗]
属性使用枚举,所以在这种情况下,至少在某种程度上自我记录。否则,我不知所措。
There have been other comments in regard to what the [Flags]
attribute does not do but other than "it affects the ToString()
result" no one seems to know or wants to explain what it does do. In code I write, I adorn enumerations that I intend to use as bitfields with the [Flags]
attribute, so in that case it is at least somewhat self-documenting. Otherwise, I'm at a loss.
这篇关于按位或组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!