1. Trang chủ
  2. » Công Nghệ Thông Tin

Beginning Regular Expressions 2005 phần 6 doc

78 231 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 78
Dung lượng 3,71 MB

Nội dung

Figure 15-16 Figure 15-17 The Custom AutoFilter offers advantages over the data form in some respects. For example, it allows the user to specify that a pattern occurs at the beginning or end of a cell’s value. This gives functionality similar to the ^ and $ metacharacters. In addition, AND or OR logic can be used to create two rules. However, the Custom AutoFilter allows patterns to be defined for only one column. The data form crite- ria can be defined on any combination of the columns in the list. Exercises 1. How can you display in NamesWithFilter.xls only names where the first name begins with Kar? 2. In Months.xls, describe how you would match only cells that contain the character sequence Jun or the character sequence Jul. 363 Wildcards in Microsoft Excel 18_574892 ch15.qxd 1/7/05 10:59 PM Page 363 18_574892 ch15.qxd 1/7/05 10:59 PM Page 364 16 Regular Expression Functionality in SQL Server 2000 Vast quantities of business and other data are stored in SQL Server, Microsoft’s premier relational database product. Most administrators and developers who use SQL Server are familiar with the problems that can occur when retrieving data from a very large data store. The better a developer or user understands the data, the better the retrieval of data is achieved. The sensitivity and speci- ficity of the retrieval of desired data from SQL Server can be improved by using regular expression functionality. Regular expression–like functionality can be achieved using the LIKE keyword in a WHERE clause or by using SQL Server’s full-text indexing and search capability. In this chapter, you will learn the following: ❑ Which metacharacters are supported in SQL Server 2000 ❑ How to use the supported metacharacters with the LIKE keyword ❑ How to achieve regular expression–like functionality using full-text search The examples in this chapter assume that you have SQL Server 2000 installed as a local, unnamed instance. If you are running SQL Server 2000 as a named instance, modify the connection informa- tion accordingly, if necessary. This chapter describes functionality in SQL Server 2000. Similar functionality is present in the beta of SQL Server 2005. 19_574892 ch16.qxd 1/7/05 10:56 PM Page 365 Metacharacters Supported SQL Server 2000 supports a limited number of metacharacters, some of which have nonstandard usage and meaning. Each of the four metacharacters is used in the context of the LIKE keyword. The metacharacters supported in SQL Server 2000 are listed in the following table. Metacharacter Meaning % Matches zero or more characters. % is not a quantifier. _ The underscore character matches a single character. It is not a quantifier. [ ] Matches a character class. Character class ranges are supported. [^ ] Matches any character except those in the character class. Many aspects of regular expressions are not supported for use with the LIKE keyword. The following table lists regular expressions features that are not supported. Metacharacter or Functionality Comment \d Not supported \w Not supported Back references Not supported ? Not supported * Not supported; the % metacharacter is not a quantifier + Not supported {n,m} Not supported Lookahead Not supported Using LIKE with Regular Expressions The LIKE keyword is used in a WHERE clause which, in turn, is part of a SELECT statement. The LIKE keyword allows the WHERE clause to filter on a regular expression pattern, rather than simply on a literal character sequence. The Try It Out sections that follow look at examples that make use of the limited collection of meta- characters that SQL Server 2000 supports. The % Metacharacter The % metacharacter matches zero or more characters. The % metacharacter is equivalent to the meta- sequence .* in more standard regular expression implementations. 366 Chapter 16 19_574892 ch16.qxd 1/7/05 10:56 PM Page 366 Try It Out The % Metacharacter 1. Open Query Analyzer. In Windows XP, select Start ➪ All Programs ➪ SQL Server ➪ Query Analyzer. 2. From the Connect to SQL Server dialog box, connect to the appropriate SQL Server. The Query Analyzer should open with an appearance similar to that shown in Figure 16-1. The appearance may vary depending on previous use of Query Analyzer and option settings. 3. In the first query, you will use the pubs sample database and will select authors whose last name begins with B. You can do this by using the pattern B%, where the metacharacter % matches any number of any character or combination of characters. Type the following Transact-SQL code into the query pane of the Query Analyzer: USE pubs SELECT au_lname, au_fname FROM dbo.authors WHERE au_lname LIKE ‘B%’ ORDER BY au_lname Figure 16-1 367 Regular Expression Functionality in SQL Server 2000 19_574892 ch16.qxd 1/7/05 10:56 PM Page 367 4. Press F5 or click the blue right-pointing arrow above the query pane to run the Transact-SQL code. Figure 16-2 shows the appearance after Step 4. Notice that the last name and first name of authors whose surname begins with B are displayed in the results pane, in the lower part of the figure. If you have mistyped the Transact-SQL code, an error message may appear in the results pane. If you cannot find the error, the code is included in the file BSurnames.sql. 5. To match surnames that contain the letter B, either case, occurring at the beginning of the sur- name or later, you can use the pattern %b%. Type the following Transact-SQL code into the query pane of the Query Analyzer: USE pubs SELECT au_lname, au_fname FROM dbo.authors WHERE au_lname LIKE ‘%b%’ ORDER BY au_lname 6. Press F5 or click the blue right-pointing arrow above the query pane to run the Transact-SQL code. Figure 16-3 shows the appearance. Notice that the last names of the authors in the result set each contain a letter b, either at the beginning of the word or later in the word. If you have difficulty when you type the code in, you can use the file BAnywhere.sql as an alternative way to run the code. Figure 16-2 368 Chapter 16 19_574892 ch16.qxd 1/7/05 10:56 PM Page 368 Figure 16-3 7. To find last names that contain the character sequence nt, you can use the pattern %nt%. Type the following Transact-SQL code into the query pane of the Query Analyzer: USE pubs SELECT au_lname, au_fname FROM dbo.authors WHERE au_lname LIKE ‘%nt%’ ORDER BY au_lname 8. Press F5 or click the blue right-pointing arrow above the query pane to run the Transact-SQL code. Figure 16-4 shows the appearance. Notice that each of the surnames contains the character sequence nt. The file NTanywhere.sql contains the code if you prefer not to type it into the query pane. 9. The LIKE keyword can be used more than once in the same WHERE clause. For example, if you wanted to find authors with surnames that begin with R and a first name that begins with the character sequence Al, you could use the pattern R% in relation to the au_lname column and the pattern Al% in relation to the au_fname column. 369 Regular Expression Functionality in SQL Server 2000 19_574892 ch16.qxd 1/7/05 10:56 PM Page 369 Type the following code into the query pane in Query Analyzer: USE pubs SELECT au_lname, au_fname FROM dbo.authors WHERE au_lname LIKE ‘R%’ AND au_fname LIKE ‘Al%’ ORDER BY au_lname 10. Press F5 or click the blue right-pointing arrow above the query pane to run the Transact-SQL code. Figure 16-5 shows the appearance. On this occasion, only one name is returned that satis- fies both criteria. 11. You can combine the LIKE keyword with the NOT keyword. For example, to select authors whose surname does not begin with B, you can use a WHERE clause like this: WHERE au_lname NOT LIKE ‘B%’ Type the following code into the query pane of the Query Analyzer: USE pubs SELECT au_lname, au_fname FROM dbo.authors WHERE au_lname NOT LIKE ‘B%’ ORDER BY au_lname Figure 16-4 370 Chapter 16 19_574892 ch16.qxd 1/7/05 10:56 PM Page 370 Figure 16-5 12. Press F5 or click the blue right-pointing arrow above the query pane to run the Transact-SQL code. When the code is run, all authors in the pubs database are returned except for those whose surname begins with B. How It Works The pattern B% matches the character B or b when it occurs at the beginning of a value and is then fol- lowed by any number of any characters. Occurrences of the character b, either case, that occur after the beginning of the word are not matched. The pattern %b% matches the character B or b anywhere in a last name. The % at the beginning of %b% matches zero or more characters. When it matches zero characters, it can match surnames beginning with B. The pattern %nt% combines a character sequence nt with a preceding % metacharacter and a following % metacharacter. Any surname containing the character sequence nt is matched. 371 Regular Expression Functionality in SQL Server 2000 19_574892 ch16.qxd 1/7/05 10:56 PM Page 371 [...]... there is only one option.) Figure 16- 15 6 On the next screen, check the check boxes for the title and notes columns, and click the Next button Figure 16- 16 shows the screen where the selection of columns to index is made 382 Figure 16- 16 Regular Expression Functionality in SQL Server 2000 7 8 9 10 Choose a name for the catalog I called the one I created on my machine Chap 16 Optionally, you can choose a... metacharacters, some derived from SQL syntax and some from regular expressions syntax The following tables summarize regular expression support in MySQL 4.0 The first table lists the SQL metacharacters that are used with the LIKE keyword The second table lists the regular expression metacharacters that are used with the REGEXP keyword 3 96 Using Regular Expressions with MySQL The following metacharacters are... the pattern 1_ in the third line of the Transact-SQL code 372 Regular Expression Functionality in SQL Server 2000 Figure 16- 6 4 Press F5 or click the blue right-pointing arrow above the query pane to run the Transact-SQL code Figure 16- 7 shows the appearance after this step Notice that the first three rows that were displayed in Figure 16- 6 are no longer displayed How It Works The pattern 1% matches... Indexing The initial screen of the Full-Text Indexing Wizard, shown in Figure 16- 14, is displayed Regular Expression Functionality in SQL Server 2000 Figure 16- 13 Figure 16- 14 381 Chapter 16 5 Click the Next button There is only one possible index on the titles table, so no other option is available from the drop-down list Figure 16- 15 shows the screen where you may be offered a choice about which column... Figure 16- 19 The message tells you that the index has not yet been populated Assuming that you have opted in Step 11 to do a full population at a time only a few minutes away, you may want to wait until that time arrives and the population takes place 383 Chapter 16 Figure 16- 18 Figure 16- 19 14 With the pubs database selected in Enterprise Manager, double-click the Full-Text Indexes option If Chap 16 (or... FORMSOF(INFLECTIONAL,computer)’) This differs from standard regular expressions where character sequences are matched with no understanding of English word forms being necessary The INFLECTIONAL keyword operates by using such knowledge of word forms in English 390 Regular Expression Functionality in SQL Server 2000 Document Filters on Image Columns In image columns, SQL Server 2000 can store documents of various types For example,... right-pointing arrow above the query pane to run the Transact-SQL code Figure 16- 11 shows the appearance after this step Notice that the rows containing the last name Adams are not displayed They would be displayed between rows 6 and 7 if they had been matched, given the ORDER BY clause Figure 16- 10 377 Chapter 16 Figure 16- 11 5 Negated character classes can be used with ranges For example, if you wanted... correctly, you won’t be able to work through the following examples Assuming that all has gone well, you are now ready to explore full-text search functionality 384 Regular Expression Functionality in SQL Server 2000 Figure 16- 20 Figure 16- 21 385 Chapter 16 Using The CONTAINS Predicate The CONTAINS predicate, which is part of SQL Server Full-Text Search, is used in the WHERE clause of a Transact-SQL SELECT statement... content of those columns, you can use the following Transact-SQL code to view the relevant information: USE pubs SELECT title_id, title, notes FROM titles Figure 16- 22 shows the appearance after the preceding code has been run Figure 16- 22 3 86 Regular Expression Functionality in SQL Server 2000 The following exercise searches for titles that contain the word computer Try It Out The CONTAINS Predicate... “psychology” ‘) 5 Press F5 or click the blue right-pointing arrow to run the code Figure 16- 24 shows the appearance after this step Notice that only one row is displayed, whose title contains both the words computer and psychology Similarly, OR logic can be used Figure 16- 24 388 Regular Expression Functionality in SQL Server 2000 6 Type the following code into the query pane: USE pubs SELECT title_id, title, . equivalent to the meta- sequence .* in more standard regular expression implementations. 366 Chapter 16 19_574892 ch 16. qxd 1/7/05 10: 56 PM Page 366 Try It Out The % Metacharacter 1. Open Query Analyzer Figure 16- 14, is displayed. 380 Chapter 16 19_574892 ch 16. qxd 1/7/05 10: 56 PM Page 380 Figure 16- 13 Figure 16- 14 381 Regular Expression Functionality in SQL Server 2000 19_574892 ch 16. qxd 1/7/05. Transact-SQL code. 372 Chapter 16 19_574892 ch 16. qxd 1/7/05 10: 56 PM Page 372 Figure 16- 6 4. Press F5 or click the blue right-pointing arrow above the query pane to run the Transact-SQL code. Figure 16- 7 shows the

Ngày đăng: 13/08/2014, 12:21

TỪ KHÓA LIÊN QUAN