CS253: Software Development with C++

Spring 2021

Regular Expressions

Regular Expressions Lab                

In this lab, you will gain practice using regular expressions. A skeleton source file is in ~cs253/Lab/RegularExpressions/regex.cc. Copy it to results.cc, modify that, and turn that in.                 

Template                

The file ~cs253/Lab/RegularExpressions/regex.cc contains:

#include <iostream>
#include <regex>

using namespace std;

// Match a regular expression (pattern) against a fixed string,
// and show the match, if any.

void test(int test_number, string pattern) {
    const string target("A quick brown fox jumps over the lazy dog");

    cout << test_number << ": pattern=\"" << pattern << "\""
         << " target=\"" << target << "\"" << endl;

    const regex r(pattern);         // Compile regexp into internal form.
    smatch m;                       // Match information goes here.

    if (regex_search(target, m, r))
        cout << "   at " << m.position() << ": \"" << m.str() << "\"\n";
    else
        cout << "   no match\n";
}

int main() {

    // Exercise 1
    // Match the string "jump"--nothing else.
    // Look, I did it for you!
    test(1, "jump");

    // Exercise 2
    // Match the string "A", but only at the start of the line.
    test(2, "regex goes here");

    // Exercise 3
    // Match the string "lazy dog", but only at the end of the line.
    test(3, "regex goes here");

    // Exercise 4
    // Match the letter v, followed by any single character, followed by r.
    test(4, "regex goes here");

    // Exercise 5
    // Match "fo", followed by any string of any length,
    // including the empty string, followed by "er".
    test(5, "regex goes here");

    // Exercise 6
    // Match "ro", followed by any number of w’s,
    // but it must be at least one, followed by "n":
    test(6, "regex goes here");

    // Exercise 7
    // Match "bro", followed by an optional "q", followed by "w".
    test(7, "regex goes here");

    // Exercise 8
    // Match a word, at least four letters long, that doesn’t contain "i".
    test(8, "regex goes here");

    // Exercise 9
    // Match "the ", followed by either "lazy" or "old", followed by " dog".
    test(9, "regex goes here");

    // Exercise 10
    // Match "o", followed by any single character "m" through "x",
    // followed any single character "a" through "f".
    test(10, "regex goes here");

    // Exercise 11
    // Match a single character that isn’t "f" or "g",
    // followed by "o", followed by a letter "a" through "m".
    test(11, "regex goes here");

    // Exercise 12
    // Match an entire single word that ends with "e".
    test(12, "regex goes here");

    // Exercise 13
    // Match three consecutive words, where the middle word begins with "f".
    test(13, "regex goes here");

    return 0;
}

Exercises                

regex.cc contains a number of calls to the function test(), which takes a regular expression, matches it against the string A quick brown fox jumps over the lazy dog, and displays the location and text of any match.                 

You will replace each placeholder regular expression "regex goes here" with a regular expression that performs the task described in the previous lines. The first exercise is already done.                 

    1: pattern="jump" target="A quick brown fox jumps over the lazy dog"
       at 18: "jump"
The 18 means that “jump” was found at offset 18 of the target string.

How to submit your work:                

In Canvas, check in the file results.cc to the assignment “Lab15”. It’s due 10:00:00ᴘᴍ MT Saturday, with a 24-hour late period for a 25% penalty.                 

How to receive negative points:                

Turn in someone else’s work.