A regular expression can extract a structured part of a text record when the format is predictable.

regex `std::regex` describes a text pattern.
match group Parentheses in a regex capture part of a match so the program can use it separately.

Regex Extract Code

ticket
regex_extract_code.cpp
Replay: real traced execution (multi-file project)
#include <iostream>
#include <regex>
#include <string>

int main() {
    std::string ticket = "BUG-204";

    std::regex pattern("([A-Z]+)-([0-9]+)");
    std::smatch match;
    bool ok = std::regex_match(ticket, match, pattern);
    std::string prefix = ok ? match[1].str() : "none";
    std::string number = ok ? match[2].str() : "0";

    std::cout << "ok=" << ok << std::endl;
    std::cout << "prefix=" << prefix << std::endl;
    std::cout << "number=" << number << std::endl;
    return 0;
}
#include <iostream>
#include <regex>
#include <string>

int main() {
    std::string ticket = "TASK-77";

    std::regex pattern("([A-Z]+)-([0-9]+)");
    std::smatch match;
    bool ok = std::regex_match(ticket, match, pattern);
    std::string prefix = ok ? match[1].str() : "none";
    std::string number = ok ? match[2].str() : "0";

    std::cout << "ok=" << ok << std::endl;
    std::cout << "prefix=" << prefix << std::endl;
    std::cout << "number=" << number << std::endl;
    return 0;
}
#include <iostream>
#include <regex>
#include <string>

int main() {
    std::string ticket = "note";

    std::regex pattern("([A-Z]+)-([0-9]+)");
    std::smatch match;
    bool ok = std::regex_match(ticket, match, pattern);
    std::string prefix = ok ? match[1].str() : "none";
    std::string number = ok ? match[2].str() : "0";

    std::cout << "ok=" << ok << std::endl;
    std::cout << "prefix=" << prefix << std::endl;
    std::cout << "number=" << number << std::endl;
    return 0;
}
  1. ticket ← BUG-204, pattern ← (empty), match ← (empty), ok ← 1, prefix ← BUG

    5int main() {6    std::string ticket→ BUG-204 = "BUG-204"; //@ticket="TASK-77", "note"78    std::regex pattern→ (empty)("([A-Z]+)-([0-9]+)");9    std::smatch match→ (empty);10    bool ok→ 1 = std::regex_match(ticketBUG-204, match(empty), pattern(empty));11    std::string prefix→ BUG = ok1 ? match(empty)[1].str() : "none";12    std::string number→ 204 = ok1 ? match(empty)[2].str() : "0";1314    std::cout << "ok=" << ok1 << std::endl;15    std::cout << "prefix=" << prefixBUG << std::endl;16    std::cout << "number=" << number204 << std::endl;17    return 0;18}
    outputok=1
    prefix=BUG
    number=204
  1. ticket ← TASK-77, pattern ← (empty), match ← (empty), ok ← 1, prefix ← TASK

    5int main() {6    std::string ticket→ TASK-77 = "TASK-77";78    std::regex pattern→ (empty)("([A-Z]+)-([0-9]+)");9    std::smatch match→ (empty);10    bool ok→ 1 = std::regex_match(ticketTASK-77, match(empty), pattern(empty));11    std::string prefix→ TASK = ok1 ? match(empty)[1].str() : "none";12    std::string number→ 77 = ok1 ? match(empty)[2].str() : "0";1314    std::cout << "ok=" << ok1 << std::endl;15    std::cout << "prefix=" << prefixTASK << std::endl;16    std::cout << "number=" << number77 << std::endl;17    return 0;18}
    outputok=1
    prefix=TASK
    number=77
  1. ticket ← note, pattern ← (empty), match ← (empty), ok ← 0, prefix ← none

    5int main() {6    std::string ticket→ note = "note";78    std::regex pattern→ (empty)("([A-Z]+)-([0-9]+)");9    std::smatch match→ (empty);10    bool ok→ 0 = std::regex_match(ticketnote, match(empty), pattern(empty));11    std::string prefix→ none = ok0 ? match(empty)[1].str() : "none";12    std::string number→ 0 = ok0 ? match(empty)[2].str() : "0";1314    std::cout << "ok=" << ok0 << std::endl;15    std::cout << "prefix=" << prefixnone << std::endl;16    std::cout << "number=" << number0 << std::endl;17    return 0;18}
    outputok=0
    prefix=none
    number=0