Lab Exercise regex0

This is a lab exercise on developing secure software. For more information, see the introduction to the labs.

Goal

Learn how to create simple regular expressions.

Background

Regular expressions (regexes) are a widely-used notation for expressing text patterns. We will later see how to use regexes to validate input.

Different regex languages have slightly different notations, but they have much in common. Here are some basic rules for regex notations:

  1. The most trivial rule is that a letter or digit matches itself. That is, the regex “d” matches the letter “d”. Most implementations use case-sensitive matches by default, and that is usually what you want.
  2. Another rule is that square brackets surround a rule that specifies any of a number of characters. If the square brackets surround just alphanumerics, then the pattern matches any of them. So [brt] matches a single “b”, “r”, or “t”. Inside the brackets you can include ranges of symbols separated by dash ("-"), so [A-D] will match one character, which can be one A, one B, one C, or one D. You can do this more than once. For example, the term [A-Za-z] will match one character, which can be an uppercase Latin letter or a lowercase Latin letter. (This text assumes you're not using a long-obsolete character system like EBCDIC.)
  3. If you follow a pattern with “*”, that means “0 or more times”. In almost all regex implementations (except POSIX BRE), following a pattern with "+" means "1 or more times". So [A-D]* will match 0 or more letters as long as every letter is an A, B, C, or D.

Task Information

We're going to create some simple regular expressions (regexes) to look for certain patterns. Use the “hint” and “give up” buttons if necessary.

Interactive Lab ()

Please create regular expression (regex) patterns that meet the criteria below.

Part 1

Create a regular expression, for use in ECMAScript (JavaScript), that searches for the term "cat" anywhere in a string.


Part 2

Create a regular expression, for use in ECMAScript (JavaScript), that searches for one or more "A" followed by one or more "B".




This lab was developed by David A. Wheeler at The Linux Foundation.