[英]Why RegexOptions are compiled to RegexOptions.None in MSIL?
This code 这段代码
Regex regex = new Regex("blah", RegexOptions.Singleline & RegexOptions.IgnoreCase);
after compilation looks like this in ILSpy: 编译后在ILSpy中看起来像这样:
Regex regex = new Regex("blah", RegexOptions.None);
Why does it happen and can it be the reason of regex not matching in .Net 3.5? 为什么会发生这种情况,并且它是正则表达式在.Net 3.5中不匹配的原因吗? On 4.5 it works. 在4.5它工作。
RegexOptions.Singleline & RegexOptions.IgnoreCase
is a bitwise AND, and resolves to 0 (ie RegexOptions.None
). 是按位AND,并解析为0(即RegexOptions.None
)。
The RegexOptions
enum looks like this: RegexOptions
枚举如下所示:
[Flags]
public enum RegexOptions
{
None = 0,
IgnoreCase = 1,
Multiline = 2,
ExplicitCapture = 4,
Compiled = 8,
Singleline = 16,
IgnorePatternWhitespace = 32,
RightToLeft = 64,
ECMAScript = 256,
CultureInvariant = 512,
}
So, in binary, we have: 所以,在二进制中,我们有:
RegexOptions.SingleLine == 10000
RegexOptions.IngoreCase == 00001
When applying a bitwise AND, we get : 当应用按位AND时,我们得到:
10000
AND 00001
-----
00000
Replace with 用。。。来代替
RegexOptions.Singleline | RegexOptions.IgnoreCase
which gives: 这使:
10000
OR 00001
-----
10001
That ILSpy will decompile in: ILSpy将反编译:
Regex regex = new Regex("blah", RegexOptions.Singleline | RegexOptions.IgnoreCase);
But I don't know what "works" in .Net 4.5. 但我不知道.Net 4.5中的“有效”。 I just compiled your code, and ILSpy also outputs: 我刚编译了你的代码,ILSpy也输出:
Regex regex = new Regex("blah", RegexOptions.None);
as intended. 如预期。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.