Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning Regular Expressions 2005.pdf
Скачиваний:
101
Добавлен:
17.08.2013
Размер:
25.42 Mб
Скачать

Chapter 18

Figure 18-15

Using the # Metacharacter

The # metacharacter matches a numeric digit. It is equivalent to the \d metacharacter in more standard regular expression syntax.

The query Number in ItemTitle demonstrates how the # metacharacter can be used.

Try It Out

Matching a Numeric Digit

1.Open Access, and open the AuctionPurchases.mdb database.

2.In the database objects window, select Queries, and click the New button.

3.Select the dBeachPurchases table, click the Add button, and then click the Close button.

4.In the leftmost column in the design window, select ItemTitle in the Field row.

5.In the Criteria row in that column, enter the following code:

LIKE “*#*”

The # metacharacter will match in any ItemTitle column if a numeric digit is present.

6.In the next column, select ItemAuthor in the Field row, and save the query as Number in ItemTitle.

7.Close the query in design view. Double-click the Number in ItemTitle query, and inspect the results, as shown in Figure 18-16.

Notice that the title of each row returned contains one (or more) numeric digits. Because there is only one numeric digit in the pattern *#*, there needs to be only one numeric digit to achieve a successful match and, therefore, for the row to be displayed.

Figure 18-16

424

Regular Expressions and Microsoft Access

Using the # Character with Date/Time Data

The # character has another use in Access queries: to match values of Date/Time. Strictly speaking, this isn’t using # as a regular expression character. But you may, nevertheless, find the technique useful when matching dates of interest.

Try It Out

Using the # Character with Date/Time Data

1.Open Access, and open the AuctionPurchases.mdb database.

2.In the database objects window, select Queries in the left pane, and click the New button.

3.In the Show Table dialog box, select the dBeachPurchases table, and click the Add button.

4.In the left column, select the ItemTitle column in the Field row of the grid.

5.In the next column, select the ItemAuthor column.

6.In the next column, select the Date column.

7.In the Criteria row of that column, enter the following code:

Between #4/1/2003# And #4/30/2003#

The preceding code assumes U.S.-style dates — that is, month followed by day of the month followed by year. That is simply how Access works; it assumes MM/DD/YYYY format irrespective of locale settings.

8.Save the query as April 2003 Purchases, and close the query in design view.

9.Double-click April 2003 Purchases in the database objects window, and inspect the results. Figure 18-17 shows the items purchased in April 2003. From the Date column, you can confirm that only purchases made in April 2003 are displayed.

Figure 18-17

425

Chapter 18

Using Character Classes in Access

Microsoft Access supports character classes, including ranges and negated character classes. The normal syntax of using square brackets to enclose the character class is used in Access. However, negated character classes are indicated by an exclamation mark following the first square bracket. So if you don’t want to match the characters N through Z, the negated character class [!N-Z] would be an appropriate pattern. Outside the square brackets that contain a character class, the exclamation mark is simply a literal character.

This book uses the term character class to refer to the collection of characters contained in square brackets. You will also see the term character list used when referring to character classes in Access.

Try It Out

Using a Positive Character Class

1.Open the AuctionPurchases.mdb database in Access, and select Queries in the left pane of the database objects window.

2.Click the New button, and select Design View from the options offered. Select dBeachPurchases.

3.In the left column, select ItemTitle.

4.In the Criteria row, enter the following code:

LIKE “[A-D]*”

5.In the next column, select ItemAuthor.

6.Save the query as Titles Beginning A to D, and close the query.

7.Double-click Titles Beginning A to D in the Queries pane to run the query, and inspect the results, as shown in Figure 18-18. Notice that all of the titles displayed have their initial character in the range A through D.

Adding an ORDER BY clause can aid you in reading the results by ensuring that data is ordered in a specified way.

Figure 18-18

426

Regular Expressions and Microsoft Access

8.Right-click Titles Beginning A to D, and select the Design View option in the context menu.

9.When the design view window opens, use the drop-down list in the toolbar to switch to SQL View.

The code will look like this:

SELECT dBeachPurchases.ItemTitle, dBeachPurchases.ItemAuthor

FROM dBeachPurchases

WHERE (((dBeachPurchases.ItemTitle) Like “[A-D]*”));

10.Immediately before the final semicolon of the SQL code, insert the following code:

ORDER BY dBeachPurchases.ItemTitle

The completed code should look like this:

SELECT dBeachPurchases.ItemTitle, dBeachPurchases.ItemAuthor

FROM dBeachPurchases

WHERE (((dBeachPurchases.ItemTitle) Like “[A-D]*”))

ORDER BY dBeachPurchases.ItemTitle;

11.Save (using Ctrl+S) the amended query, and close it.

12.In the database objects window, double-click the Titles Beginning A to D query. As you can see in Figure 18-19, the titles are now ordered. It is now apparent that only titles beginning with the characters A through D are displayed.

Figure 18-19

Try It Out

Using a Negated Character Class

Follow the steps for the preceding example with the following amendments.

In Step 4, enter the following code:

LIKE “[!A-D]*”

This will create the negated character class.

427