Mastering Regular Expressions (2nd Edition) [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Mastering Regular Expressions (2nd Edition) [Electronic resources] - نسخه متنی

Jeffrey E. F. Friedl

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید










9.4 Static "Convenience" Functions


As we saw in the "Regex Quickstart" beginning in Section 9.2.1, 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 (see Section 3.2.2). 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 commonlycalled
function), there's some overhead involved in preparing the regex each time
(see Section 6.4.3). 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 save a separate Regex object for every little regex
you'd like to use can be extremely cumbersome and inconvenient, so it's wonderful
that the .NET regex package employs regex caching. If you use a patter
n/option combination that has already been used during the execution of the
program, the internal Regex object that had been built the first time is reused, saving
you the drudgery of having to save and manage the Regex object.

.NET's regex caching seems to be very efficient, so I would feel comfortable using
the convenience functions in most places. There is a small amount of overhead, as
the cache must compare the pattern string and its list of options to those it already
has, but that's a small tradeoff for the enhanced program readability of the lesscomplicated
approach that convenience functions offer. I'd still opt for building
and managing a raw Regex object in very time-sensitive situations, such as applying
regexes in a tight loop.


/ 83