Study guide: Regular expressions simplify pattern-matching code

Brush up on Java terms, learn tips and cautions, review homework assignments, and read answers to student questions

Glossary of terms

back reference
A regex construct, specified as a backslash character (\) followed by a digit character denoting a capturing group number, that recalls a capturing group's captured text characters.
boundary matcher
A regex construct that identifies a match location.
capturing group
A regex construct, specified as a sequence of characters surrounded by parentheses metacharacters (()), that captures a match's characters for later recall.
character class
A regex construct that identifies a set of characters between open and close square bracket metacharacters ([]).
character sequences
Objects whose classes implement the java.lang.CharSequence interface and serve as text sources.
embedded flag expression
A regex construct, specified as parentheses metacharacters surrounding a question mark metacharacter (?) followed by a specific lowercase letter, that overrides a matcher default.
line terminator
A one- or two-character sequence that identifies the end of a line of text.
matchers
Matcher objects.
matches
Strings that match a regex's pattern.
metacharacters
Characters that have special meaning instead of a literal meaning.
noncapturing group
A regex construct, specified as a sequence of characters surrounded by parentheses metacharacters, that does not capture text characters.
pattern
A template. Pattern objects are also known as patterns.
quantifier
A regex construct that implicitly or explicitly binds a numeric value to a pattern.
quote
Convert from meta status to literal status.
pattern matching
The process of searching text to identify matches.
regex constructs
Regex pattern categories.
regular expression
A string whose pattern describes a set of strings. Also known as a regex or regexp.
zero-length match
A match of zero length where the start and end indexes are the same.

Tips and cautions

These tips and cautions will help you write better programs and save you from agonizing over why the compiler produces error messages.

Tips

  • To specify . or any metacharacter as a literal character in a regex construct, quote—convert from meta status to literal status—the metacharacter in one of two ways:
    • Precede the metacharacter with a backslash character.
    • Place the metacharacter between \Q and \E (e.g., \Q.\E).
    In either scenario, don't forget to double each backslash character (as in \\. or \\Q.\\E) that appears in a string literal (e.g., String regex = "\\.";). Do not double the backslash character when it appears as part of a command-line argument.
  • Combine multiple ranges within the same range character class by placing them side by side. Example: [a-zA-Z] matches all lowercase and uppercase alphabetic characters.
  • To specify multiple embedded flag expressions in a regex, either place them side by side (e.g., (?m)(?i)) or place their lowercase letters side by side (e.g., (?mi)).

Cautions

  • Do not assume that Pattern's and Perl 5's regex constructs are identical. Although they share many similarities, they also share differences, ranging from disparities in the constructs they support to their treatment of dangling metacharacters. (For more information, examine Pattern's SDK documentation.)

Homework

  • Not specifying the closing parentheses metacharacter in an embedded flag expression is one example of an illegal pattern. Identify two other illegal pattern examples.
  • Create a regex that matches phone numbers with or without area codes. If present, a three-digit area code must appear between parentheses characters. Optional space characters may appear between the area code and the number. The number consists of three digit characters followed by a hyphen character followed by four digit characters. No space characters may appear on either side of the hyphen. Example: (123) 555-4678.

Answers to last month's homework

Last month, I asked you to answer two questions. Here are my answers (which appear in red).

  • What is the purpose of InstallAnywhere's Web Installer applet?

    InstallAnywhere's Web Installer applet downloads and executes installers from the Web. Consult the InstallAnywhere 5 Standard Edition user guide to learn more about this applet, such as the applet's signed and unsigned versions, and the ability to customize the Webpage that launches and configures the applet.

  • Use the Advanced Designer to add a license agreement and your own billboard advertisement to the GLA.EXE installer.

    InstallAnywhere regards a license agreement as a combination of a license agreement panel and a custom text file. Complete the following steps to add a license agreement to the GLA.EXE installer:

    1. Select the Pre-Install tab.
    2. Click the Add Action button.
    3. Select the Panels tab on the Choose An Action dialog box.
    4. Highlight the Panel: License Agreement line and click the Add button. Click the Close button to close the dialog box.
    5. A Panel: License Agreement: License Agreement line appears in the Pre-Install Action List section on the Pre-Install tab. This line should immediately follow the Panel: Introduction: Introduction line. (Why perform any installation tasks if the user will not accept the license agreement?) Use the up/down arrow buttons to position Panel: License Agreement: License Agreement appropriately.
    6. Ensure Panel: License Agreement: License Agreement is highlighted so the Panel: License Agreement section appears on the Pre-Install tab.
    7. Modify the license agreement title, user prompt, the custom license agreement text file path, and default acceptance setting in the Panel: License Agreement section.

    InstallAnywhere regards a billboard advertisement as either a gif, jpeg, or png file; with a maximum size of either 380 x 270 pixels (Swing GUI (graphical user interface) mode) or 587 x 312 pixels (AWT (Abstract Windowing Toolkit) GUI mode with no side panels). Complete the following steps to add a billboard advertisement to the GLA.EXE installer:

    1. Select the Project tab.
    2. Select the Billboards tab on the Project tab.
    3. Click the Add Billboard button.
    4. Select the appropriate image file from the Choose An Image File dialog box and click the Open button.
    5. Use the up/down arrow buttons to position the highlighted image filename in the Billboard List section on the Billboards tab. InstallAnywhere displays billboard advertisements in the top to bottom sequence that Billboard List specifies.