Basics of regular expressions



Introduction to using regular expressions

A regular expression describes a pattern of characters. Regular expressions are typically used to verify that a text value conforms to a particular pattern (such as verifying that a user-entered phone number has the proper number of digits) or to replace portions of a text value that matches a particular pattern.

Regular expressions can be simple. For example, suppose you wanted to confirm that a particular string matches “ABC,” or wanted to replace every occurrence of “ABC” in a string with some other text. In that case, you could use the following regular expression, which defines the pattern consisting of the letters A, B, and C in sequence:

/ABC/

Note that the regular expression literal is delineated with the forward slash (/) character.

Regular expression patterns can also be complex, and sometimes cryptic in appearance, such as the following expression to match a valid e-mail address:

/([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}/

Most commonly you will use regular expressions to search for patterns in strings and to replace characters. In those cases, you will create a regular expression object and use it as a parameter for one of several String class methods. The following methods of the String class take regular expressions as parameters: match(), replace(), search(), and split(). For more information on these methods, see Finding patterns in strings and replacing substrings.

The RegExp class includes the following methods: test() and exec(). For more information, see Methods for using regular expressions with strings.

Common regular expression tasks

There are several common uses for regular expressions, which are described in detail in this chapter:

  • Creating a regular expression pattern

  • Using special characters in patterns

  • Identifying sequences of multiple characters (such as “a two-digit number” or “between seven and ten letters”)

  • Identifying any character in a range of letters or numbers (such as “any letter from a to m” )

  • Identifying a character in a set of possible characters

  • Identifying subsequences (segments within a pattern)

  • Matching and replacing text based on patterns

Important concepts and terms

The following reference list contains important terms used in this chapter:

  • Escape character: A character indicating that the character that follows should be treated as a metacharacter rather than a literal character. In regular expression syntax, the backslash character (\) is the escape character, so a backslash followed by another character is a special code rather than just the character itself.

  • Flag: A character that specifies some option about how the regular expression pattern should be used, such as whether to distinguish between uppercase and lowercase characters.

  • Metacharacter: A character that has special meaning in a regular expression pattern, as opposed to literally representing that character in the pattern.

  • Quantifier: A character (or several characters) indicating how many times a part of the pattern should repeat. For example, a quantifier would be used to designate that a United States postal code should contain five or nine numbers.

  • Regular expression: A program statement defining a pattern of characters that can be used to confirm whether other strings match that pattern or to replace portions of a string.

Working through in-chapter examples

As you’re working through the chapter, you may want to test some of the example code listings for yourself. Because the code listings in this chapter consist primarily of regular expression patterns, testing the examples involves a few steps:

  1. Create a new Flash document.

  2. Select a keyframe and open the Actions panel.

  3. Create a RegExp (regular expression) variable such as this one:

    var pattern:RegExp = /ABC/;
  4. Copy the pattern from the example and assign it as the value of your RegExp variable. For instance, in the previous line of code, the pattern is the part of the code to the right of the equals sign, not including the semicolon (/ABC/).

  5. Create one or more String variables containing strings appropriate for testing your regular expression. For instance, if you are creating a regular expression to test for valid e-mail addresses, create a few String variables containing valid and bad e-mail addresses:

    var goodEmail:String = "bob@example.com"; 
    var badEmail:String = "5@$2.99";
  6. Add lines of code to test the String variables to determine whether they match the regular expression pattern. These will be the values that you’ll want to output to the screen using the trace() function or by writing them to a text field on the Stage.

    trace(goodEmail, " is valid:", pattern.test(goodEmail)); 
    trace(badEmail, " is valid:", pattern.test(badEmail));

    For instance, assuming pattern defines the regular expression pattern for a valid e-mail address, the preceding lines of code writes this text to the Output panel:

    bob@example.com is valid: true 
    5@$2.99 is valid: false

    For more information about testing values by writing the values into a text field instance on the Stage or by using the trace() function to print the values to the Output panel, see Testing in-chapter example code listings.