C++ Academy · Lesson

regex Basics

Match patterns in text.

Lesson 1 of 413 steps

regex Basics is a free C++ Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.

What Is <regex>?

The <regex> header lets you match text against regular expressions patterns that describe sets of strings. You build a pattern, then test or search text with it.

  • std::regex holds a compiled pattern.
  • Matching functions report whether text fits.
#include <iostream>
#include <regex>

int main() {
    std::regex pattern("hello");
    std::cout << std::boolalpha << std::regex_match("hello", pattern) << '\n';
    return 0;
}

regex_match vs regex_search

regex_match requires the entire string to match. regex_search succeeds if the pattern appears anywhere in the string.

#include <iostream>
#include <regex>

int main() {
    std::regex p("cat");
    std::string s = "the cat sat";
    std::cout << std::boolalpha;
    std::cout << "match: " << std::regex_match(s, p) << '\n';
    std::cout << "search: " << std::regex_search(s, p) << '\n';
    return 0;
}

Character Classes

Square brackets define a set of allowed characters. [aeiou] matches any single vowel; ranges like [a-z] match lowercase letters.

#include <iostream>
#include <regex>

int main() {
    std::regex p("[a-z]+");
    std::cout << std::boolalpha << std::regex_match("hello", p) << '\n';
    std::cout << std::regex_match("Hello", p) << '\n';
    return 0;
}

Quantifiers

Quantifiers control repetition: * zero or more, + one or more, ? optional, and {n,m} a count range.

#include <iostream>
#include <regex>

int main() {
    std::regex p("a+b");
    std::cout << std::boolalpha;
    std::cout << std::regex_match("aaab", p) << '\n';
    std::cout << std::regex_match("b", p) << '\n';
    return 0;
}

Predefined Classes

Shorthand classes save typing: \\d matches a digit, \\w a word character, \\s whitespace. In C++ source the backslash is doubled.

#include <iostream>
#include <regex>

int main() {
    std::regex p("\\d+");
    std::cout << std::boolalpha;
    std::cout << std::regex_match("12345", p) << '\n';
    std::cout << std::regex_match("12a45", p) << '\n';
    return 0;
}

Anchors

Anchors pin a match to a position: ^ marks the start and $ the end of the text. They match positions, not characters.

#include <iostream>
#include <regex>

int main() {
    std::regex p("^abc$");
    std::cout << std::boolalpha;
    std::cout << std::regex_search("abc", p) << '\n';
    std::cout << std::regex_search("xabc", p) << '\n';
    return 0;
}

Alternation

The pipe | means "or". The pattern cat|dog matches either word. Group with parentheses to limit its scope.

#include <iostream>
#include <regex>

int main() {
    std::regex p("cat|dog");
    std::cout << std::boolalpha;
    std::cout << std::regex_match("cat", p) << '\n';
    std::cout << std::regex_match("dog", p) << '\n';
    std::cout << std::regex_match("fish", p) << '\n';
    return 0;
}

Escaping Special Characters

Characters like ., *, and ( have special meaning. Escape them with a backslash to match them literally.

#include <iostream>
#include <regex>

int main() {
    std::regex p("3\\.14");
    std::cout << std::boolalpha;
    std::cout << std::regex_match("3.14", p) << '\n';
    std::cout << std::regex_match("3x14", p) << '\n';
    return 0;
}

Case-Insensitive Matching

Pass a flag such as std::regex::icase when constructing the pattern to ignore letter case during matching.

#include <iostream>
#include <regex>

int main() {
    std::regex p("hello", std::regex::icase);
    std::cout << std::boolalpha;
    std::cout << std::regex_match("HELLO", p) << '\n';
    std::cout << std::regex_match("Hello", p) << '\n';
    return 0;
}

A Simple Validation

Patterns are great for validating input formats, like checking that a string looks like a basic number with optional sign.

#include <iostream>
#include <regex>

bool isNumber(const std::string& s) {
    static const std::regex p("-?\\d+");
    return std::regex_match(s, p);
}

int main() {
    std::cout << std::boolalpha;
    std::cout << isNumber("-42") << '\n';
    std::cout << isNumber("4a2") << '\n';
    return 0;
}

Matching Multiple Times

Combine classes and quantifiers to validate richer formats, such as a sequence of letters followed by digits.

#include <iostream>
#include <regex>

int main() {
    std::regex p("[A-Za-z]+\\d+");
    std::cout << std::boolalpha;
    std::cout << std::regex_match("abc123", p) << '\n';
    std::cout << std::regex_match("123abc", p) << '\n';
    return 0;
}

Quick Check

Test your understanding of match versus search.

Recap

You learned the basics of <regex>:

  • regex_match for whole-string, regex_search for substring matches
  • character classes, quantifiers, anchors, and alternation
  • predefined classes like \\d and escaping special characters
  • flags such as icase

Next, you will extract specific parts of a match using capturing groups.

Free to start

Learn C++ with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “regex Basics” lesson free?

Yes — any 3 lessons of the C++ Academy learning path, including “regex Basics”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.

What will I learn in “regex Basics”?

Match patterns in text. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “regex Basics” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. regex Basics
  2. Capturing Groups
  3. Search and Replace
  4. Performance Notes
← Back to C++ Academy