0Pricing
C++ Academy · Lesson

regex Basics

Match patterns in text.

regex Basics is a free C++ Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “regex Basics” lesson free?

Yes — the full text of “regex Basics” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

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