Lecture 16: Regular Expressions in C++ | CMSC 240 Software Systems Development - Fall 2024

Lecture 16: Regular Expressions in C++

Objective

Equip students with an understanding of regular expressions and their practical application in C++ programming, enabling them to proficiently utilize regular expressions for pattern matching, text manipulation, and data extraction in real-world programming scenarios.

Module 8: https://classroom.github.com/a/zRXzMlfT

Lecture Topics

Introduction to Regular Expressions

What are regular expressions?

Why are they important in text processing?

Real-world examples of where regular expressions are used:

C++ Regular Expression Library

The <regex> header in C++:

Raw Strings in C++

In C++, a raw string is a literal string that is defined using the R"( ... )" syntax. This syntax allows you to create string literals without the need for escaping special characters, making it especially useful for strings that contain backslashes and multiple lines of text.

Here’s the basic syntax of a raw string in C++:

R"( your raw string here )"

Here are some key features and benefits of raw strings in C++:

Here’s an example of a raw string in C++ that includes a backslash and multiple lines of text:

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    string rawString = R"(This is a raw string.
It can contain backslashes \ and span multiple lines.
No need to escape special characters.)";

    cout << rawString << endl;

    return 0;
}

In this example, the R"( ... )" syntax is used to define a raw string, which can span multiple lines and includes backslashes without the need for escaping.

Raw strings can be especially handy when working with regular expressions, file paths, SQL queries, or any text that includes special characters or spans multiple lines. They improve code readability by eliminating the clutter of escape sequences and line concatenations.

Like this:

regex pattern(R"(your_regular_expression_here)");

Example: Validating an Email Address

A simple program to validate an email address from input.

validate.cpp

#include <iostream>
#include <regex>
#include <string>
using namespace std;

int main() 
{
    string email;
    cout << "Enter an email address: ";
    cin >> email;

    // Define a regular expression pattern for email validation
    regex pattern{R"([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})"};

    if (regex_match(email, pattern)) 
    {
        cout << "Valid email address!" << endl;
    } 
    else
    {
        cout << "Invalid email address." << endl;
    }

    return 0;
}

Regular Expressions Tutorial

https://regexone.com/

Complete lessons 1 through 14.

Resources

Testing your regex: regex101.com

Reference page: regular-expressions