Visual Studio Hacks [Electronic resources] نسخه متنی

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

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

Visual Studio Hacks [Electronic resources] - نسخه متنی

Andrew Lockhart

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

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

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







Hack 100. Test Regular Expressions in Visual Studio

Regular expressions can be much trickier than
the rest of your code. Break the edit-compile-debug-shriek cycle with
a free add-in that tests regular expressions right inside Visual
Studio.

Regular expressions are strings of symbols and
characters that are used to identify a part or parts of strings.
Regular expressions are a very powerful and flexible technology, but
they are also a very challenging technology. The easiest way to more
efficiently work with regular expressions is to find a utility that
allows you to quickly write and evaluate them. Some wonderful Windows
utilities, including Regulator (http://royo.is-a-geek.com/iserializable/regulator)
and Expresso (http://www.codeproject.com/dotnet/expresso.asp),
are available. But both of these tools require you to run a separate
application, and a free add-in called
RegExinator
lets you write regular expressions and then run them against the
current document in Visual Studio. The add-in is not as full featured
as the standalone Windows utilities, but it can be more convenient
since it is built directly into Visual Studio.


I have a small personal interest in this add-in, as I helped Peter
Wright in the development of this tool.

First, you will need to download and install the add-in from
http://www.visualstudiohacks.com/regexinator.


13.10.1. Evaluate Regular Expressions


The main use for the
RegExinator is to evaluate
regular expressions directly inside of Visual Studio. To show an
example, let's say that you have a text file with
names and phone numbers, and you need to retrieve a list of all the
area codes from this text file. This is a perfect example of how to
apply regular expressions. Here is a sample of this file:

James Avery, (513) 555-1212
Tammy Avery, (513) 555-1212
Louisa Avery, (615) 555-1212

To use RegExinator, you will need to load this file into Visual
Studio and then open the RegExinator window, which is shown in Figure 13-24.


Figure 13-24. RegExinator Tool Window

Once you have the file loaded and the tool window open, you can enter
a regular expression. For this example, if you wanted to get all of
the area codes, you could use a regular expression that looks
something like this:

(\([0-9]{3}\))

The first ( is the opening of the regular
expression. The \( is an escape character for the
first parenthesis of the area code. The [0-9]
specifies any number between 0 and 9, and the {3}
states that you want three instances of this number. The
\) is the closing parenthesis of the area code,
and finally ) is the end of the regular
expression.

You can enter this regular expression into the tool and then click
the Run button. RegExinator will then process the current document
using the regular expression and show the results, as shown in Figure 13-25.


Figure 13-25. RegExinator results

You can select any of the matches, and that line will be highlighted
in the document. You can also search only a selection in your
document by selecting that section of the document and then running
the tool. RegExinator will then look for matches only in the part of
the document you have selected.


13.10.2. Find More Regular Expressions


The other functionality built into
RegExinator is the ability to search the Regular Expressions Library
from right inside Visual Studio. The Regular Expression
Library (http://www.regexlib.com)
is a web site that contains a plethora of extremely useful regular
expressions cataloged by their use. It is a great resource to know
about when trying to figure out a complicated regular expression. If
you wanted to find a regular expression that would match both United
States and European style phone numbers, you would have no problem
finding one in the library. RegExLib also exposes a great set of Web
Services that can be used to search the library; these services are
what makes integration with RegExinator possible.

To search the library using RegExinator, you simply need to click the
RegExLib tab and you will see a simple text box labeled Search. Enter
your search criteria in this text box and click the Run button, and
you will then see the results as shown in Figure 13-26.


Figure 13-26. RegExLib search results

Each node in the window is a regular expression that was found in
RegExLib. Below each node are examples of what the regular expression
will match, what it won't match, and a brief
description of the expression. When you have found a regular
expression that you want to test, you simply need to select the
expression and then click the Test button. This will copy the
expression to the Tester window where you can then run it against
your document.

RegExinator is a simple but useful tool for evaluating regular
expressions and searching for expressions from inside Visual Studio.
For more information on regular expressions, the authoritative text
on the subject is Mastering Regular Expressions
(O'Reilly)


/ 172