- •Introduction
- •Who This Book Is For
- •What This Book Covers
- •How This Book Is Structured
- •What You Need to Use This Book
- •Conventions
- •Source Code
- •Errata
- •p2p.wrox.com
- •What Are Regular Expressions?
- •What Can Regular Expressions Be Used For?
- •Finding Doubled Words
- •Checking Input from Web Forms
- •Changing Date Formats
- •Finding Incorrect Case
- •Adding Links to URLs
- •Regular Expressions You Already Use
- •Search and Replace in Word Processors
- •Directory Listings
- •Online Searching
- •Why Regular Expressions Seem Intimidating
- •Compact, Cryptic Syntax
- •Whitespace Can Significantly Alter the Meaning
- •No Standards Body
- •Differences between Implementations
- •Characters Change Meaning in Different Contexts
- •Regular Expressions Can Be Case Sensitive
- •Case-Sensitive and Case-Insensitive Matching
- •Case and Metacharacters
- •Continual Evolution in Techniques Supported
- •Multiple Solutions for a Single Problem
- •What You Want to Do with a Regular Expression
- •Replacing Text in Quantity
- •Regular Expression Tools
- •findstr
- •Microsoft Word
- •StarOffice Writer/OpenOffice.org Writer
- •Komodo Rx Package
- •PowerGrep
- •Microsoft Excel
- •JavaScript and JScript
- •VBScript
- •Visual Basic.NET
- •Java
- •Perl
- •MySQL
- •SQL Server 2000
- •W3C XML Schema
- •An Analytical Approach to Using Regular Expressions
- •Express and Document What You Want to Do in English
- •Consider the Regular Expression Options Available
- •Consider Sensitivity and Specificity
- •Create Appropriate Regular Expressions
- •Document All but Simple Regular Expressions
- •Document What You Expect the Regular Expression to Do
- •Document What You Want to Match
- •Test the Results of a Regular Expression
- •Matching Single Characters
- •Matching Sequences of Characters That Each Occur Once
- •Introducing Metacharacters
- •Matching Sequences of Different Characters
- •Matching Optional Characters
- •Matching Multiple Optional Characters
- •Other Cardinality Operators
- •The * Quantifier
- •The + Quantifier
- •The Curly-Brace Syntax
- •The {n} Syntax
- •The {n,m} Syntax
- •Exercises
- •Regular Expression Metacharacters
- •Thinking about Characters and Positions
- •The Period (.) Metacharacter
- •Matching Variably Structured Part Numbers
- •Matching a Literal Period
- •The \w Metacharacter
- •The \W Metacharacter
- •Digits and Nondigits
- •The \d Metacharacter
- •Canadian Postal Code Example
- •The \D Metacharacter
- •Alternatives to \d and \D
- •The \s Metacharacter
- •Handling Optional Whitespace
- •The \S Metacharacter
- •The \t Metacharacter
- •The \n Metacharacter
- •Escaped Characters
- •Finding the Backslash
- •Modifiers
- •Global Search
- •Case-Insensitive Search
- •Exercises
- •Introduction to Character Classes
- •Choice between Two Characters
- •Using Quantifiers with Character Classes
- •Using the \b Metacharacter in Character Classes
- •Selecting Literal Square Brackets
- •Using Ranges in Character Classes
- •Alphabetic Ranges
- •Use [A-z] With Care
- •Digit Ranges in Character Classes
- •Hexadecimal Numbers
- •IP Addresses
- •Reverse Ranges in Character Classes
- •A Potential Range Trap
- •Finding HTML Heading Elements
- •Metacharacter Meaning within Character Classes
- •The ^ metacharacter
- •How to Use the - Metacharacter
- •Negated Character Classes
- •Combining Positive and Negative Character Classes
- •POSIX Character Classes
- •The [:alnum:] Character Class
- •Exercises
- •String, Line, and Word Boundaries
- •The ^ Metacharacter
- •The ^ Metacharacter and Multiline Mode
- •The $ Metacharacter
- •The $ Metacharacter in Multiline Mode
- •Using the ^ and $ Metacharacters Together
- •Matching Blank Lines
- •Working with Dollar Amounts
- •Revisiting the IP Address Example
- •What Is a Word?
- •Identifying Word Boundaries
- •The \< Syntax
- •The \>Syntax
- •The \b Syntax
- •The \B Metacharacter
- •Less-Common Word-Boundary Metacharacters
- •Exercises
- •Grouping Using Parentheses
- •Parentheses and Quantifiers
- •Matching Literal Parentheses
- •U.S. Telephone Number Example
- •Alternation
- •Choosing among Multiple Options
- •Unexpected Alternation Behavior
- •Capturing Parentheses
- •Numbering of Captured Groups
- •Numbering When Using Nested Parentheses
- •Named Groups
- •Non-Capturing Parentheses
- •Back References
- •Exercises
- •Why You Need Lookahead and Lookbehind
- •The (? metacharacters
- •Lookahead
- •Positive Lookahead
- •Negative Lookahead
- •Positive Lookahead Examples
- •Positive Lookahead in the Same Document
- •Inserting an Apostrophe
- •Lookbehind
- •Positive Lookbehind
- •Negative Lookbehind
- •How to Match Positions
- •Adding Commas to Large Numbers
- •Exercises
- •What Are Sensitivity and Specificity?
- •Extreme Sensitivity, Awful Specificity
- •Email Addresses Example
- •Replacing Hyphens Example
- •The Sensitivity/Specificity Trade-Off
- •Sensitivity, Specificity, and Positional Characters
- •Sensitivity, Specificity, and Modes
- •Sensitivity, Specificity, and Lookahead and Lookbehind
- •How Much Should the Regular Expressions Do?
- •Abbreviations
- •Characters from Other Languages
- •Names
- •Sensitivity and How to Achieve It
- •Specificity and How to Maximize It
- •Exercises
- •Documenting Regular Expressions
- •Document the Problem Definition
- •Add Comments to Your Code
- •Making Use of Extended Mode
- •Know Your Data
- •Abbreviations
- •Proper Names
- •Incorrect Spelling
- •Creating Test Cases
- •Debugging Regular Expressions
- •Treacherous Whitespace
- •Backslashes Causing Problems
- •Considering Other Causes
- •The User Interface
- •Metacharacters Available
- •Quantifiers
- •The @ Quantifier
- •The {n,m} Syntax
- •Modes
- •Character Classes
- •Back References
- •Lookahead and Lookbehind
- •Lazy Matching versus Greedy Matching
- •Examples
- •Character Class Examples, Including Ranges
- •Whole Word Searches
- •Search-and-Replace Examples
- •Changing Name Structure Using Back References
- •Manipulating Dates
- •The Star Training Company Example
- •Regular Expressions in Visual Basic for Applications
- •Exercises
- •The User Interface
- •Metacharacters Available
- •Quantifiers
- •Modes
- •Character Classes
- •Alternation
- •Back References
- •Lookahead and Lookbehind
- •Search Example
- •Search-and-Replace Example
- •Online Chats
- •POSIX Character Classes
- •Matching Numeric Digits
- •Exercises
- •Introducing findstr
- •Finding Literal Text
- •Quantifiers
- •Character Classes
- •Command-Line Switch Examples
- •The /v Switch
- •The /a Switch
- •Single File Examples
- •Simple Character Class Example
- •Find Protocols Example
- •Multiple File Example
- •A Filelist Example
- •Exercises
- •The PowerGREP Interface
- •A Simple Find Example
- •The Replace Tab
- •The File Finder Tab
- •Syntax Coloring
- •Other Tabs
- •Numeric Digits and Alphabetic Characters
- •Quantifiers
- •Back References
- •Alternation
- •Line Position Metacharacters
- •Word-Boundary Metacharacters
- •Lookahead and Lookbehind
- •Longer Examples
- •Finding HTML Horizontal Rule Elements
- •Matching Time Example
- •Exercises
- •The Excel Find Interface
- •Escaping Wildcard Characters
- •Using Wildcards in Data Forms
- •Using Wildcards in Filters
- •Exercises
- •Using LIKE with Regular Expressions
- •The % Metacharacter
- •The _ Metacharacter
- •Character Classes
- •Negated Character Classes
- •Using Full-Text Search
- •Using The CONTAINS Predicate
- •Document Filters on Image Columns
- •Exercises
- •Using the _ and % Metacharacters
- •Testing Matching of Literals: _ and % Metacharacters
- •Using Positional Metacharacters
- •Using Character Classes
- •Quantifiers
- •Social Security Number Example
- •Exercises
- •The Interface to Metacharacters in Microsoft Access
- •Creating a Hard-Wired Query
- •Creating a Parameter Query
- •Using the ? Metacharacter
- •Using the * Metacharacter
- •Using the # Metacharacter
- •Using the # Character with Date/Time Data
- •Using Character Classes in Access
- •Exercises
- •The RegExp Object
- •Attributes of the RegExp Object
- •The Other Properties of the RegExp Object
- •The test() Method of the RegExp Object
- •The exec() Method of the RegExp Object
- •The String Object
- •Metacharacters in JavaScript and JScript
- •SSN Validation Example
- •Exercises
- •The RegExp Object and How to Use It
- •Quantifiers
- •Positional Metacharacters
- •Character Classes
- •Word Boundaries
- •Lookahead
- •Grouping and Nongrouping Parentheses
- •Exercises
- •The System.Text.RegularExpressions namespace
- •A Simple Visual Basic .NET Example
- •The Classes of System.Text.RegularExpressions
- •The Regex Object
- •Using the Match Object and Matches Collection
- •Using the Match.Success Property and Match.NextMatch Method
- •The GroupCollection and Group Classes
- •The CaptureCollection and Capture Class
- •The RegexOptions Enumeration
- •Case-Insensitive Matching: The IgnoreCase Option
- •Multiline Matching: The Effect on the ^ and $ Metacharacters
- •Right to Left Matching: The RightToLeft Option
- •Lookahead and Lookbehind
- •Exercises
- •An Introductory Example
- •The Classes of System.Text.RegularExpressions
- •The Regex Class
- •The Options Property of the Regex Class
- •Regex Class Methods
- •The CompileToAssembly() Method
- •The GetGroupNames() Method
- •The GetGroupNumbers() Method
- •GroupNumberFromName() and GroupNameFromNumber() Methods
- •The IsMatch() Method
- •The Match() Method
- •The Matches() Method
- •The Replace() Method
- •The Split() Method
- •Using the Static Methods of the Regex Class
- •The IsMatch() Method as a Static
- •The Match() Method as a Static
- •The Matches() Method as a Static
- •The Replace() Method as a Static
- •The Split() Method as a Static
- •The Match and Matches Classes
- •The Match Class
- •The GroupCollection and Group Classes
- •The RegexOptions Class
- •The IgnorePatternWhitespace Option
- •Metacharacters Supported in Visual C# .NET
- •Using Named Groups
- •Using Back References
- •Exercise
- •The ereg() Set of Functions
- •The ereg() Function
- •The ereg() Function with Three Arguments
- •The eregi() Function
- •The ereg_replace() Function
- •The eregi_replace() Function
- •The split() Function
- •The spliti() Function
- •The sql_regcase() Function
- •Perl Compatible Regular Expressions
- •Pattern Delimiters in PCRE
- •Escaping Pattern Delimiters
- •Matching Modifiers in PCRE
- •Using the preg_match() Function
- •Using the preg_match_all() Function
- •Using the preg_grep() Function
- •Using the preg_quote() Function
- •Using the preg_replace() Function
- •Using the preg_replace_callback() Function
- •Using the preg_split() Function
- •Supported Metacharacters with ereg()
- •Using POSIX Character Classes with PHP
- •Supported Metacharacters with PCRE
- •Positional Metacharacters
- •Character Classes in PHP
- •Documenting PHP Regular Expressions
- •Exercises
- •W3C XML Schema Basics
- •Tools for Using W3C XML Schema
- •Comparing XML Schema and DTDs
- •How Constraints Are Expressed in W3C XML Schema
- •W3C XML Schema Datatypes
- •Derivation by Restriction
- •Unicode and W3C XML Schema
- •Unicode Overview
- •Using Unicode Character Classes
- •Matching Decimal Numbers
- •Mixing Unicode Character Classes with Other Metacharacters
- •Unicode Character Blocks
- •Using Unicode Character Blocks
- •Metacharacters Supported in W3C XML Schema
- •Positional Metacharacters
- •Matching Numeric Digits
- •Alternation
- •Using the \w and \s Metacharacters
- •Escaping Metacharacters
- •Exercises
- •Introduction to the java.util.regex Package
- •Obtaining and Installing Java
- •The Pattern Class
- •Using the matches() Method Statically
- •Two Simple Java Examples
- •The Properties (Fields) of the Pattern Class
- •The CASE_INSENSITIVE Flag
- •Using the COMMENTS Flag
- •The DOTALL Flag
- •The MULTILINE Flag
- •The UNICODE_CASE Flag
- •The UNIX_LINES Flag
- •The Methods of the Pattern Class
- •The compile() Method
- •The flags() Method
- •The matcher() Method
- •The matches() Method
- •The pattern() Method
- •The split() Method
- •The Matcher Class
- •The appendReplacement() Method
- •The appendTail() Method
- •The end() Method
- •The find() Method
- •The group() Method
- •The groupCount() Method
- •The lookingAt() Method
- •The matches() Method
- •The pattern() Method
- •The replaceAll() Method
- •The replaceFirst() Method
- •The reset() Method
- •The start() Method
- •The PatternSyntaxException Class
- •Using the \d Metacharacter
- •Character Classes
- •The POSIX Character Classes in the java.util.regex Package
- •Unicode Character Classes and Character Blocks
- •Using Escaped Characters
- •Using Methods of the String Class
- •Using the matches() Method
- •Using the replaceFirst() Method
- •Using the replaceAll() Method
- •Using the split() Method
- •Exercises
- •Obtaining and Installing Perl
- •Creating a Simple Perl Program
- •Basics of Perl Regular Expression Usage
- •Using the m// Operator
- •Using Other Regular Expression Delimiters
- •Matching Using Variable Substitution
- •Using the s/// Operator
- •Using s/// with the Global Modifier
- •Using s/// with the Default Variable
- •Using the split Operator
- •Using Quantifiers in Perl
- •Using Positional Metacharacters
- •Captured Groups in Perl
- •Using Back References in Perl
- •Using Alternation
- •Using Character Classes in Perl
- •Using Lookahead
- •Using Lookbehind
- •Escaping Metacharacters
- •A Simple Perl Regex Tester
- •Exercises
- •Index
Chapter 3
Figure 3-13
Matching Optional Characters
Matching literal characters is straightforward, particularly when you are aiming to match exactly one literal character for each corresponding literal character that you include in a regular expression pattern. The next step up from that basic situation is where a single literal character may occur zero times or one time. In other words, a character is optional. Most regular expression dialects use the question mark (?) character to indicate that the preceding chunk is optional. I am using the term “chunk” loosely here to mean the thing that precedes the question mark. That chunk can be a single character or various, more complex regular expression constructs. For the moment, we will deal with the case of the single, optional character. More complex regular expression constructs, such as groups, are described in Chapter 7.
For example, suppose you are dealing with a group of documents that contain both U.S. English and British English.
You may find that words such as color (in U.S. English) appear as colour (British English) in some documents. You can express a pattern to match both words like this:
colou?r
You may want to standardize the documents so that all the spellings are U.S. English spellings.
Try It Out |
Matching an Optional Character |
Try this out using the Komodo Regular Expression Toolkit:
1.Open the Komodo Regular Expression Toolkit ,and clear any regular expression pattern or text that may have been retained.
2.Insert the text colour into the area for the text to be matched.
3.Enter the regular expression pattern colou?r into the area for the regular expression pattern. The text colour is matched, as shown in Figure 3-14.
56
Simple Regular Expressions
Figure 3-14
Try this regular expression pattern with text such as that shown in the sample file Colors.txt:
Red is a color.
His collar is too tight or too colouuuurful.
These are bright colours.
These are bright colors.
Calorific is a scientific term.
“Your life is very colorful,” she said.
How It Works
The word color in the line Red is a color. will match the pattern colou?r.
When the regular expression engine reaches a position just before the c of color, it attempts to match a lowercase c. This match succeeds. It next attempts to match a lowercase o. That too matches. It next attempts to match a lowercase l and a lowercase o. They match as well. It then attempts to match the pattern u?, which means zero or one lowercase u characters. Because there are exactly zero lowercase u characters following the lowercase o, there is a match. The pattern u? matches zero characters. Finally, it attempts to match the final character in the pattern — that is, the lowercase r. Because the next character in the string color does match a lowercase r, the whole pattern is matched.
There is no match in the line His collar is too tight or too colouuuurful. The only possible match might be in the sequence of characters colouuuurful. The failure to match occurs when the regular expression engine attempts to match the pattern u?. Because the pattern u? means “match zero or one lowercase u characters,” there is a match on the first u of colouuuurful. After that successful match, the regular expression engine attempts to match the final character of the pattern colou?r against the second lowercase u in colouuuurful. That attempt to match fails, so the attempt to match the whole pattern colou?r against the sequence of characters colouuuurful also fails.
57
Chapter 3
What happens when the regular expression engine attempts to find a match in the line These are bright colours.?
When the regular expression engine reaches a position just before the c of colours, it attempts to match a lowercase c. That match succeeds. It next attempts to match a lowercase o, a lowercase l, and another lowercase o. These also match. It next attempts to match the pattern u?, which means zero or one lowercase u characters. Because exactly one lowercase u character follows the lowercase o in colours, there is a match. Finally, the regular expression engine attempts to match the final character in the pattern, the lowercase r. Because the next character in the string colours does match a lowercase r, the whole pattern is matched.
The findstr utility can also be used to test for the occurrence of the sequence of characters color and colour, but the regular expression engine in the findstr utility has a limitation in that it lacks a metacharacter to signify an optional character. For many purposes, the * metacharacter, which matches zero, one, or more occurrences of the preceding character, will work successfully.
To look for lines that contain matches for colour and color using the findstr utility, enter the following at the command line:
findstr /N colo*r Colors.txt
The preceding command line assumes that the file Colors.txt is in the current directory.
Figure 3-15 shows the result from using the findstr utility on Colors.txt.
Figure 3-15
Notice that lines that contain the sequences of characters color and colour are successfully matched, whether as whole words or parts of longer words. However, notice, too, that the slightly strange “word” colouuuurful is also matched due to the * metacharacter’s allowing multiple occurrences of the lowercase letter u. In most practical situations, such bizarre “words” won’t be an issue for you, and the * quantifier will be an appropriate substitute for the ? quantifier when using the findstr utility. In some situations, where you want to match precisely zero or one specific characters, the findstr utility may not provide the functionality that you need, because it would also match a character sequence such as colonifier.
Having seen how we can use a single optional character in a regular expression pattern, let’s look at how you can use multiple optional characters in a single regular expression pattern.
58
Simple Regular Expressions
Matching Multiple Optional Characters
Many English words have multiple forms. Sometimes, it may be necessary to match all of the forms of a word. Matching all those forms can require using multiple optional characters in a regular expression pattern.
Consider the various forms of the word color (U.S. English) and colour (British English). They include the following:
color (U.S. English, singular noun)
colour (British English, singular noun)
colors (U.S. English, plural noun)
colours (British English, plural noun)
color’s (U.S. English, possessive singular)
colour’s (British English, possessive singular)
colors’ (U.S. English, possessive plural)
colours’ (British English, possessive plural)
The following regular expression pattern, which include three optional characters, can match all eight of these word forms:
colou?r’?s?’?
If you tried to express this in a semiformal way, you might have the following problem definition:
Match the U.S. English and British English forms of color (colour), including the singular noun, the plural noun, and the singular possessive and the plural possessive.
Let’s try it out, and then I will explain why it works and what limitations it potentially has.
Try It Out Matching Multiple Optional Characters
Use the sample file Colors2.txt to explore this example:
These colors are bright.
Some colors feel warm. Other colours feel cold.
A color’s temperature can be important in creating reaction to an image.
These colours’ temperatures are important in this discussion.
Red is a vivid colour.
59
Chapter 3
To test the regular expression, follow these steps:
1.Open OpenOffice.org Writer, and open the file Colors2.txt.
2.Use the keyboard shortcut Ctrl+F to open the Find and Replace dialog box.
3.Check the Regular Expressions check box and the Match Case check box.
4.In the Search for text box, enter the regular expression pattern colou?r’?s?’?, and click the Find All button. If all has gone well, you should see the matches shown in Figure 3-16.
Figure 3-16
As you can see, all the sample forms of the word of interest have been matched.
How It Works
In this description, I will focus initially on matching of the forms of the word colour/color.
How does the pattern colou?r’?s?’? match the word color? Assume that the regular expression engine is at the position immediately before the first letter of color. It first attempts to match lowercase c, because one lowercase c must be matched. That matches. Attempts are then made to match a subsequent
60
Simple Regular Expressions
lowercase o, l, and o. These all also match. Then an attempt is made to match an optional lowercase u. In other words, zero or one occurrences of the lowercase character u is needed. Because there are zero occurrences of lowercase u, there is a match. Next, an attempt is made to match lowercase r. The lowercase r in color matches. Then an attempt is made to match an optional apostrophe. Because there is no occurrence of an apostrophe, there is a match. Next, the regular expression engine attempts to match an optional lowercase s — in other words, to match zero or one occurrence of lowercase s. Because there is no occurrence of lowercase s, again, there is a match. Finally, an attempt is made to match an optional apostrophe.
Because there is no occurrence of an apostrophe, another match is found. Because a match exists for all the components of the regular expression pattern, there is a match for the whole regular expression pattern colour?r’?s?’?.
Now, how does the pattern colou?r’?s?’? match the word colour? Assume that the regular expression engine is at the position immediately before the first letter of colour. It first attempts to match lowercase c, because one lowercase c must be matched. That matches. Next, attempts are made to match a subsequent lowercase o, l, and another o. These also match. Then an attempt is made to match an optional lowercase u. In other words, zero or one occurrences of the lowercase character u are needed. Because there is one occurrence of lowercase u, there is a match. Next, an attempt is made to match lowercase r. The lowercase r in colour matches. Next, the engine attempts to match an optional apostrophe. Because there is no occurrence of an apostrophe, there is a match. Next, the regular expression engine attempts to match an optional lowercase s — in other words, to match zero or one occurrences of lowercase s. Because there is no occurrence of lowercase s, a match exists. Finally, an attempt is made to match an optional apostrophe. Because there is no occurrence of an apostrophe, there is a match. All the components of the regular expression pattern have a match; therefore, the entire regular expression pattern colour?r’?s?’? matches.
Work through the other six word forms shown earlier, and you’ll find that each of the word forms does, in fact, match the regular expression pattern.
The pattern colou?r’?s?’? matches all eight of the word forms that were listed earlier, but will the pattern match the following sequence of characters?
colour’s’
Can you see that it does match? Can you see why it matches the pattern? If each of the three optional characters in the regular expression is present, the preceding sequence of characters matches. That rather odd sequence of characters likely won’t exist in your sample document, so the possibility of false matches (reduced specificity) won’t be an issue for you.
How can you avoid the problem caused by such odd sequences of characters as colour’s’? You want to be able to express is something like this:
Match a lowercase c. If a match is present, attempt to match a lowercase o. If that match is present, attempt to match a lowercase l. If there is a match, attempt to match a lowercase o. If a match exists, attempt to match an optional lowercase u. If there is a match, attempt to match a lowercase r. If there is a match, attempt to match an optional apostrophe. And if a match exists here, attempt to match an optional lowercase s. If the earlier optional apostrophe was not present, attempt to match an optional apostrophe.
With the techniques that you have seen so far, you aren’t able to express ideas such as “match something only if it is not preceded by something else.” That sort of approach might help achieve higher specificity at the expense of increased complexity. Techniques where matching depends on such issues are presented in Chapter 9.
61