什么时候应该使用编译正则表达式与跨preTED?什么时候、正则表达式、preTED

2023-09-03 07:21:29 作者:三更伥鬼

看完这篇文章http://www.codinghorror.com/blog/archives/000228.html不过,我明白编译正EX pressions好一点的好处,在什么情况下个人,你会考虑强制要求使用编译注册前的?

例如,我使用的是正则表达式在一个循环中,并定期前pression字符串使用不同的变量每次迭代,所以我将寻求没有起色通过标记此正则表达式的编译吧?

感谢您的答案我的实际code不是直接和被破坏建立在动态的可再生能源,所以我不能包含它,所以对于所有密集的目的,这里有一个例子说明我的方法:

 的foreach(在fields.Where场场(X => x.condition))
    MatchResults = Regex.Match(request.Message,field.RegularEx pression);
...
 

解决方案

在.NET中,有两种方法来编译一个普通的前pression。普通EX pressions总是编译,他们可以找到在比赛前。当你实例化Regex类没有RegexOptions.Compiled标志,你的正常的前pression仍然需要转换成使用的正则表达式类的内部数据结构。实际的匹配过程运行在数据结构,而不是字符串重新presenting你的正则表达式。它坚持只要你的正则表达式实例所在。

明确实例化正则表达式类是preferable来调用静态正则表达式的方法,如果您使用的是相同的正则表达式超过一次。其原因是,静态方法反正创建一个正则表达式实例,然后把它扔掉。他们保留最近编正则表达式的高速缓存,但缓存是相当小的,并且缓存查找远远超过简单地引用一个指针到现有的Regex实例昂贵

编译上述形式,使用正前pressions每一个编程语言或库中存在,但不是所有的报价控制权。

.NET Framework提供了通过构造一个正则表达式对象,并指定RegexOptions.Compiled标志编译经常EX pressions的第二种方式。缺席或$ P $这个标志的psence并不表示正则表达式是否编译。它表示正则表达式是否被编译快,如上,或详尽的描述,如下所述。

什么RegexOptions.Compiled确实是用编译成MSIL你的正常的前pression创建一个新的组件。该组件然后加载,编译成机器code,并成为您的应用程序的永久组成部分(尽管它运行)。这个过程需要大量的CPU蜱和内存使用情况是永久性的。

Python正则表达式

您应该使用RegexOptions.Compiled只有当你正在处理如此多的数据与它的用户实际上必须等待你的正则表达式。如果你不能测量用秒表的速度差,也懒得与RegexOptions.Compiled。

After reading this article http://www.codinghorror.com/blog/archives/000228.html I understand the benefits of compiled regular expressions a little better, however in what personal scenarios would you consider mandates the use of a compiled Reg Ex?

For instance, I am using a regex in a loop and the regular expression string utilises different variables each iteration, so I would seek no improvement by flagging this regex as compiled right?

Hi thanks for your answers, my actual code is not straightforward and is compromised of an RE built on the fly so I cannot include it, so for all intensive purposes, here is an example which demonstrates my approach:

foreach (field field in fields.Where(x => x.condition))
    MatchResults = Regex.Match(request.Message, field.RegularExpression);
...

解决方案

In .NET, there are two ways to "compile" a regular expression. Regular expressions are always "compiled" before they can be used to find matches. When you instantiate the Regex class without the RegexOptions.Compiled flag, your regular expression is still converted into an internal data structure used by the Regex class. The actual matching process runs on that data structure rather than string representing your regex. It persists as long as your Regex instance lives.

Explicitly instantiating the Regex class is preferable to calling the static Regex methods if you're using the same regex more than once. The reason is that the static methods create a Regex instance anyway, and then throw it away. They do keep a cache of recently compiled regexes, but the cache is rather small, and the cache lookup far more costly than simply referencing a pointer to an existing Regex instance.

The above form of compilation exists in every programming language or library that uses regular expressions, though not all offer control over it.

The .NET framework provides a second way of compiling regular expressions by constructing a Regex object and specifying the RegexOptions.Compiled flag. Absence or presence of this flag does not indicate whether or not the regex is compiled. It indicates whether the regex is compiled quickly, as described above, or thoroughly, as described below.

What RegexOptions.Compiled really does is to create a new assembly with your regular expression compiled down to MSIL. This assembly is then loaded, compiled to machine code, and becomes a permanent part of your application (while it runs). This process takes a lot of CPU ticks, and the memory usage is permanent.

You should use RegexOptions.Compiled only if you're processing so much data with it that the user actually has to wait on your regex. If you can't measure the speed difference with a stopwatch, don't bother with RegexOptions.Compiled.