9.4. Static "Convenience" Functions
As we saw in the "Regex Quickstart" beginning on page 413, you don't always have to create explicit Regex objects. The following static functions allow you to apply with regular expressions directly:
Regex.IsMatch( target, pattern ) Regex.IsMatch( target, pattern, options ) Regex.Match( target, pattern ) Regex.Match( target, pattern, options ) Regex.Matches( target, pattern ) Regex.Matches( target, pattern, options ) Regex.Replace( target, pattern, replacement ) Regex.Replace( target, pattern, replacement, options ) Regex.Split( target, pattern ) Regex.Split( target, pattern, options )
Internally, these are just wrappers around the core Regex constructor and methods we've already seen. They construct a temporary Regex object for you, use it to call the method you've requested , and then throw the object away. (Well, they don't actually throw it awaymore on this in a bit.)
Here's an example:
If Regex.IsMatch( Line , "^\s*$")
That's the same as
Dim TemporaryRegex = New Regex("^\s*$") If TemporaryRegex .IsMatch( Line )
or, more accurately, as:
If New Regex("^\s*$").IsMatch( Line )
The advantage of using these convenience functions is that they generally make simple tasks easier and less cumbersome. They allow an object-oriented package to appear to be a procedural one (˜95). The disadvantage is that the pattern must be reinspected each time.
If the regex is used just once in the course of the whole program's execution, it doesn't matter from an efficiency standpoint whether a convenience function is used. But, if a regex is used multiple times (such as in a loop, or a commonly-called function), there's some overhead involved in preparing the regex each time (˜241). The goal of avoiding this usually expensive overhead is the primary reason you'd build a Regex object once, and then use it repeatedly later when actually checking text. However, as the next section shows, .NET offers a way to have the best of both worlds : procedural convenience with object-oriented efficiency.
9.4.1. Regex Caching
Having to always build and manage a separate Regex object for every little regex you'd like to use can be cumbersome and inconvenient, so it's wonderful that the .NET regex package provides its various static methods. One efficiency downside of the static methods, though, is that each invocation in theory creates a temporary Regex object for you, applies it, and then discards it. That can be a lot of redundant work when done many times for the same regex in a loop.
To avoid that repeated work, the .NET Framework provides caching for the temporary objects created via the static methods. Caching in general is discussed in Chapter 6 (˜244), but in short, this means that if you use the same regex in a static method as you've used "recently," the static method reuses the previously-created regex object rather than building a new one from scratch.
The default meaning of "recently" is that the most recent 15 regexes are cached. If you use more than 15 regexes in a loop, the benefits of the cache are lost because the 16th regex forces out the first, so that by the time you restart the loop and reapply , the first is no longer in the cache and must be regenerated.
If the default size of 15 is too small for your needs, you can adjust it:
Regex.CacheSize = 123
Should you ever want to disable all caching, you can set the value to zero.