XML technologies / Regular expressions / .NET / Static convenience functions
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
In the “In brief” sections, we saw that explicit regex objects are not always necessary. The following static functions offer a more direct way to apply regular expressions:
Regex.IsMatch(Suchtext, Muster) Regex.IsMatch(Suchtext, Muster, Optionen)Regex.Match(Suchtext, Muster) Regex.Match(Suchtext, Muster, Optionen)Regex.Matches(Suchtext, Muster) Regex.Matches(Suchtext, Muster, Optionen)Regex.Replace(Suchtext, Muster, Ersatztext) Regex.Replace(Suchtext, Muster, Ersatztext, Optionen)Regex.Split(Suchtext, Muster) Regex.Split(Suchtext, Muster, Optionen)
Internally, these functions naturally call the Regex constructors and methods used so far, create a temporary Regex object, apply the requested method to it, and delete it afterward. (Actually, the objects are not discarded; more on that later.)
Here is an example:
If Regex.IsMatch(Zeile, "^\s*$")
...This has the same effect as
Dim TempRegex = New Regex("^\s*$")
If TempRegex.IsMatch(Zeile)
...or, more precisely, as:
If New Regex("^\s*$").IsMatch(Zeile)
...These convenience functions make simple tasks much easier. They add a procedural interface to the otherwise object-oriented regex package (see Procedural and object-oriented approaches). The drawback is that the regex must be evaluated and compiled again each time.
If a regex is used only once in the entire program, efficiency is unaffected. But when it is used repeatedly (in a loop or a frequently called function), the additional preparation overhead can matter (see Nothing comes for free!). That is precisely why the object-oriented approach builds the regex object once and reuses it. However, .NET combines the advantages of both approaches: the simplicity of procedural usage without an efficiency penalty.
Caching regular expressions
Creating a separate Regex object for every little regex is rather cumbersome. It is therefore very convenient that .NET’s static convenience methods internally use a temporary Regex object. The downside is that this temporary object is created, used, and discarded on every call; doing so in a loop is very inefficient.
.NET avoids this by using a cache of regular expressions. Caching is discussed in more detail in The art of writing regular expressions (see Compilation caching in the procedural approach). Briefly, when a regular expression has already been used with the same options, the internal Regex object stored in the cache is reused.
By default, “already used” in .NET means that the regex must match one of the last 15 used. If a loop uses more than 15, the sixteenth evicts the first from the cache, which must then be created from scratch when used again.
If the default cache size of 15 is insufficient, the user can increase it:
Regex.CacheSize = 123To disable caching entirely, set the cache size to 0.
German edition copyright © 2008 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "Reguläre Ausdrücke" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln