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

Beginning Regular Expressions 2005 phần 5 pdf

78 227 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,93 MB

Nội dung

Quantifiers The use of quantifiers in OpenOffice.org Writer is standard. The ?, *, +, and {n,m} quantifiers are all available. The test file, AandSomeBs.txt, is shown here: ABC ABBC AC A3C ABBBBBBC AbbCC The pattern AB?C matches the character sequences ABC and AC, as shown in Figure 12-3. Each of the matched character sequences consists of an uppercase A, followed by zero or one uppercase B, followed by an uppercase C. Figure 12-3 285 Regular Expressions in StarOffice/OpenOffice.org Writer 15_574892 ch12.qxd 1/7/05 10:58 PM Page 285 If the pattern is edited to AB*C, the character sequences matched are ABC, ABBC, AC, and ABBBBBBC. Each of the matched character sequences consists of an uppercase A, followed by zero or more uppercase Bs, followed by an uppercase C. If the pattern is edited to AB+C, the character sequence AC no longer matches, because it does not have an uppercase B. The pattern AB+C means “Match an uppercase A, followed by one or more uppercase Bs, followed by an uppercase C.” If the pattern is edited to AB{2,4}C, only the character sequence ABBC matches, because it is the only character sequence in the test text that has an uppercase A, followed by between two and four uppercase Bs, followed by an uppercase C. Modes OpenOffice.org Writer supports both case-insensitive (the default) and case-sensitive matching. The Match Case check box is the interface tool that controls which mode is used in matching. Character Classes The implementation of character classes in OpenOffice.org Writer is pretty much standard. Ranges are supported, as are negated character classes. OpenOffice.org Writer does not support the \d metacharacter, which matches numeric digits, or the \w metacharacter, which matches word characters. Therefore, the regular expressions author must use the corresponding character classes, [0-9] to match a numeric digit and [A-Za-z] to match both cases of alphabetic characters. The preceding character classes can be qualified by any of the quantifiers men- tioned in the preceding “Quantifiers” section. The following test text, ClassTest.txt is used in the Try It Out exercise that follows: AB1 RD2 K9 993ABC ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz 0123456789 Try It Out Character Classes 1. Open OpenOffice.org Writer, and open the test file ClassTest.txt. 2. Open the Find & Replace dialog box using the Ctrl+F keyboard shortcut. 286 Chapter 12 15_574892 ch12.qxd 1/7/05 10:58 PM Page 286 3. Check the Regular Expressions and Match Case check boxes. 4. In the Search For text box, type the pattern [0-9]. 5. Click the Find All button, and inspect the results. As shown in Figure 12-4, all the numeric digits in the test document match the character class [0-9]. 6. Edit the pattern in the Search For text box to [A-Z]. 7. Click the Find All button, and inspect the results. As shown in Figure 12-5, all the uppercase alphabetic characters in the test document match the character class [A-Z]. 8. Edit the pattern in the Search For text box to [a-z]. 9. Click the Find All button, and inspect the results. All lowercase alphabetic characters should now be highlighted as matches of the new pattern. 10. Uncheck the Match Case check box. 11. Click the Find All button, and inspect the results. As shown in Figure 12-6, both lowercase and uppercase alphabetic characters are now highlighted as matches. Figure 12-4 287 Regular Expressions in StarOffice/OpenOffice.org Writer 15_574892 ch12.qxd 1/7/05 10:58 PM Page 287 Figure 12-5 Figure 12-6 288 Chapter 12 15_574892 ch12.qxd 1/7/05 10:58 PM Page 288 How It Works The initial character class, [0-9], matches the same characters as the character class [0123456789]. The dash in [0-9] represents a range, so [0-9] represents the range of numeric digits from 0 to 9 inclusive. Because OpenOffice.org Writer does not support the \d metacharacter, which in most regular expression implementations matches numeric digits, the use of a character class to match numeric digits is needed. The character class [A-Z], similarly, matches the same characters as the character class as the pattern [ABCDEFGHIJKLMNOPQRSTUVWXYZ] but is much more succinct. Because the Match Case check box is checked (see Step 3), only uppercase alphabetic characters are matched. The character class [a-z] matches the same characters as the character class [abcdefghijklmnopqrs tuvwxyz] . With the Match Case check box checked, only lowercase alphabetic characters are matched. When the Match Case check box is unchecked in Step 10, the pattern [a-z] matches all alphabetic char- acters, both lowercase and uppercase. Alternation OpenOffice.org Writer supports the | character (often called the pipe character), which conveys the notion of alternation or the logical OR. A test document, Licenses.txt, is shown here: This licence has expired. Friday is the day that the licensing authority meets. Licences are essential before you can do that legally. License is morally questionable. Licensed practitioners only should apply. The aim is to match any occurrence of licence, license, or licensing while allowing for the possibil- ity (not occurring in the test document) that the form licencing might also be used. The problem definition can broadly be expressed as follows: Match any occurrence of the word licence or licensing, allowing for possible variations in how each word is spelled. Refining that initial attempt at a problem definition would give something like the following: Match, case insensitively, the literal character sequence l, i, c, e, n followed by either c or s, in turn followed by e or the character sequence i, n, and g. A pattern that would, when applied case insensitively, satisfy the problem definition follows: licen(c|s)(e|ing) So let’s try it out. 289 Regular Expressions in StarOffice/OpenOffice.org Writer 15_574892 ch12.qxd 1/7/05 10:58 PM Page 289 Try It Out Alternation 1. Open OpenOffice.org Writer, and open the test file Licenses.txt. 2. Check the Regular Expressions check box. Because the aim is case-insensitive matching, ensure that the Match Case check box is unchecked. 3. In the Search For text box, enter the pattern licen(c|s)(e|ing). 4. Click the Find All button, and inspect the highlighted text. Figure 12-7 shows the appearance after Step 4. You can see that the d or s at the end of Licensed and Licenses, respectively, are not matched. If the desire is to match the whole word, the pattern can be modified to achieve that. When the character sequences licens or licenc are followed by an e, you want to allow an optional choice of d or s. So licence, license, licenced, licensed, licences, and licenses would all be matched. However, when the match is licensing or licencing, you don’t want to allow an s or a d as the following character. Figure 12-7 290 Chapter 12 15_574892 ch12.qxd 1/7/05 10:58 PM Page 290 The problem definition would be modified like this: Match, case insensitively, the literal character sequence l, i, c, e, n followed by either c or s, in turn followed by either e followed by a choice of d or s, each of which is optional, or the character sequence i, n, and g. So the pattern is modified to licen(c|s)(e(s|d)?|ing) to express the preceding problem definition. As you can see from the preceding problem definition and pattern, it can become difficult to clearly express nested options. 5. Edit the pattern in the Search For text box to licen(c|s)(e(s|d)?|ing). 6. Click the Find All button, and inspect the results Figure 12-8 shows the appearance after this step. How It Works Let’s look at how the pattern licen(c|s)(e(s|d)?|ing) matches in each of the lines of the test text. Figure 12-8 291 Regular Expressions in StarOffice/OpenOffice.org Writer 15_574892 ch12.qxd 1/7/05 10:58 PM Page 291 In the first line, the relevant text that matches is licence. When the regular expression engine reaches the position immediately before the initial l of licence, the first five characters of the word match the first five literal characters of the pattern. Then the second c of licence matches the first option in (c|s). And the final e of licence matches the first option in (e(s|d)?|ing) —in other words, e(s|d)?, which is an e followed optionally by an s or a d. In the second line, the relevant text that matches is licensing. When the regular expression engine reaches the position immediately before the initial l of licensing, the first five characters of the word match the first five literal characters of the pattern. Then the s of licensing matches the second option in (c|s). Then the final character sequence ing matches the second option in (e(s|d)?|ing), which is the sequence of literal characters ing. In the third line, the relevant text that matches is Licences. Remember that the matching is being carried out case insensitively, so the initial licen of the pattern matches the initial character sequence Licen. The second c in Licences matches the first option in (c|s). The final es matches the first of the two options in (e(s|d)?|ing). In other words, it matches a literal e followed by zero or one s. The matching in the fourth line is the same, except that there is no final s to be matched. Because the (s|d)? means that the s or d is optional, there is a match. In the fifth line, the relevant text that matches is Licenced. The matching is being carried out case insen- sitively, so the initial licen of the pattern matches the initial character sequence Licen. The second c in Licenced matches the first option in (c|s). The final ed matches the first of the two options in (e(s|d)?|ing). In other words, it matches a literal e followed by zero or one d. So in this line, e(s|d)? matches the character sequence ed. Back References OpenOffice.org Writer doesn’t support back references in a standard way, but the & metacharacter pro- vides a limited back-reference-like functionality, which can be used in search and replace. Suppose that you want to modify all occurrences of words such as walk and sulk with walking, sulk- ing , and so on. You can match on the literal character sequence lk and then add ing to each such matched character sequence. The & metacharacter allows you to do that. The test file, Walk.txt, is shown here: Walk talk sulk milk 292 Chapter 12 15_574892 ch12.qxd 1/7/05 10:58 PM Page 292 Try It Out The & Metacharacter 1. Open OpenOffice.org Writer, and open the test file Walk.txt. 2. Use the Ctrl+F keyboard shortcut to open the Find & Replace dialog box. 3. Check the Regular Expressions check box. Leave the Match Case check box unchecked. 4. Enter the literal pattern lk in the Search For text box. 5. In the Replace With text box, enter the pattern &lk. 6. Click the Replace All button, and inspect the result. Figure 12-9 shows the appearance after Step 6. As you can see, each word that has the character sequence lk in it has had the character sequence ing added to it. Figure 12-9 293 Regular Expressions in StarOffice/OpenOffice.org Writer 15_574892 ch12.qxd 1/7/05 10:58 PM Page 293 [...]... Line 1 and Line 15 contain a part number beginning with A, only those lines are possible matches for the rest of the regular expression The character class [A-Z] matches any alphabetic character, 312 Regular Expressions Using findstr matching B on Line 1 and D on Line 15 The second occurrence of the character class [A-Z] in the regular expression matches C on Line 1 and F on Line 15 The three character... the beginning- of-word position and end-of-word position metacharacters mark the beginning and end of an alphabetic sequence, respectively For many practical purposes, they signify the beginning and end of a word Regular Expressions Using findstr Figure 13-12 Beginning- and End-of-Line Positions The findstr utility offers two quite distinct ways to specify that matching is to take place at the beginning. .. box 3 Check the Regular Expressions check box 4 In the Search For text box, enter the pattern ^.*Heaven.*Hell.*\ 5 Click the Find All button, and inspect the results Figure 12-10 shows the appearance after Step 5 You may be surprised to see that both sentences in the third paragraph are highlighted as matches That will be explained in the How It Works section in a moment 294 Regular Expressions in StarOffice/OpenOffice.org... 1 and 274 on Line 15 So there are matches on lines 1 and 15 In Step 6, the pattern A[^B][A-Z][0-9] is used The initial A matches on lines 1 and 15 as before The character class [^B] matches any character except uppercase B So there is no match for A[^B] on Line 1 However, on Line 15, D is a match for [^B], so matching continues on Line 15 The [A-B] pattern matches the F on Line 15, and [0-9][0-9][0-9]... file specification part of the command line /r Specifies that the text inside paired double quotes is to be interpreted as regular expressions This is the default behavior even if the /r switch is not specified /l Means that regular expressions cannot be interpreted as regular expressions Instead, matching is literal The following command-line switches each take an argument that affects their behavior:... 13 -5 shows the results Notice that all three lines containing Hello or hello are now displayed Figure 13 -5 There are some findstr command-line switches that substitute functionally for regular expressions metacharacters They will be discussed in the relevant place when the supported metacharacters are covered in the next section Metacharacters Suppor ted by findstr The findstr utility supports many regular. .. that perform functions similar to regular expression metacharacters in many other 308 Regular Expressions Using findstr settings, as well as command-line switches with other meanings Command-line switches that take arguments are described in a separate table Command-Line Switch Equivalent Metacharacter or Other Meaning /b Matches when the following character(s) are at the beginning of a line Equivalent... document on which you can use Writer regular expressions to clean up You can then save the cleaned document in Word format, using the Save As option in Writer Try It Out Tidying Up an Online Chat Transcript 1 Open OpenOffice.org Writer; then open the test file Interesting Chat.sxw 2 Open the Find & Replace dialog box using the Ctrl+F keyboard shortcut 3 Check the Regular Expressions and Match Case check... You might, for example, have to insert a space character after the ^ metacharacter if the right-arrow symbol is preceded by a space 300 Regular Expressions in StarOffice/OpenOffice.org Writer Figure 12- 15 POSIX Character Classes In addition to support for conventional regular expression character classes, OpenOffice.org Writer version 1.1 supports a subset of the POSIX character classes The supported... lowercase characters a through h and t through z 13 Regular Expressions Using findstr The findstr utility is a command-line utility that can be used to find files containing a particular string pattern The findstr utility allows searches similar to those carried out using Windows Search but also supports more specific searches that include regular expressions The findstr utility makes use of parameters . matches. Figure 12-4 287 Regular Expressions in StarOffice/OpenOffice.org Writer 15_ 574892 ch12.qxd 1/7/ 05 10 :58 PM Page 287 Figure 12 -5 Figure 12-6 288 Chapter 12 15_ 574892 ch12.qxd 1/7/ 05 10 :58 PM Page 288 How. shown in Figure 12-10. 2 95 Regular Expressions in StarOffice/OpenOffice.org Writer 15_ 574892 ch12.qxd 1/7/ 05 10 :58 PM Page 2 95 How It Works The pattern used up to Step 5 is ^.*Heaven.*Hell.*. B, followed by an uppercase C. Figure 12-3 2 85 Regular Expressions in StarOffice/OpenOffice.org Writer 15_ 574892 ch12.qxd 1/7/ 05 10 :58 PM Page 2 85 If the pattern is edited to AB*C, the character

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

TỪ KHÓA LIÊN QUAN

w