C++
'std::regex' Is Not a Member of 'std' Fix
std::regex lives in one header,
Written November 29, 2026 · Tested with GCC 13.3.0 · 12 min read
- The rule underneath: one header, C++11 or later
- The first failure: no <regex> include
- The second failure: <regex> included, compiled as C++98
- The header sweep: no header rescues in the probed set
Written November 29, 2026
Tested with GCC 13.3.0
What this guide is
The error on a std::regex name means the compiler has no regex name inside std to find. Inside a function, GCC words it exactly as this guide's title promises: 'regex' is not a member of 'std'. The common cause is a missing include: nothing in the file included <regex>, the header that declares the name, so the lookup fails at the first use. The second cause is the language mode: std::regex is since C++11, so a file compiled in an older mode gets no std::regex either - and that shape announces itself differently, with a #error from inside the library headers before the same not-a-member errors follow.
The wording also changes with where the failing name sits. At namespace scope, where the name is used as a type for a variable declaration, GCC reports 'regex' in namespace 'std' does not name a type instead. Both wordings appear below; they are one fault - the name is absent - read at two different positions in the file.
One habit from other missing-name errors does not transfer here: GCC attaches no note to this error naming the header to add. There is no "did you forget to #include" line under the std::regex error, so the error lines themselves are the whole diagnosis - the name is absent, and the remedy is the one header that declares it. And where some names are rescued by accident when a neighboring header pulls their declarations in, the sweep below finds no rescue for std::regex at all: the fix is #include <regex> in the file itself, plus a language mode of C++11 or later.
The rule underneath: one header, C++11 or later
The std::basic_regex page dates the type: the class template basic_regex is since C++11, providing a general framework for holding regular expressions, and its typedefs table gives std::regex as std::basic_regex<char>. There is no C++03 form of std::regex: the whole regular expression library arrived with the 2011 standard.
The std: the regex_error class, the basic_regex class template with the regex and wregex typedefs, the sub_match and match_results class templates with the smatch typedef for string results, and the free function templates regex_match, regex_search and regex_replace. The member list on the basic_regex page shows what a regex object itself offers: construction, assignment, mark_count, flags, locale handling and swap - no member that runs a match, because matching is the free functions' job, which is the boundary one of the neighboring-error sections below draws. The std::match_results::str page gives the accessor this guide's fix uses, with the same date: str since C++11, returning a string representing the indicated sub-match - the entire matched expression when the argument is 0, and the nth sub-match otherwise. So a std::regex name needs both conditions at once: the header included, so the declarations are in the file, and a language mode of C++11 or later, so the header is allowed to declare them.
The first failure: no <regex> include
This program uses std::regex and std::regex_search and includes no header at all - deliberately, because a companion header would be its own possible rescue, and the sweep below measures each candidate header on its own instead:
int main() {
std::regex re("a+");
return std::regex_search("aaa", re) ? 0 : 1;
}
$ g++ -std=c++11 p1_fail_no_include.cpp -o p1_fail_no_include
p1_fail_no_include.cpp: In function 'int main()':
p1_fail_no_include.cpp:2:10: error: 'regex' is not a member of 'std'
2 | std::regex re("a+");
| ^~~~~
p1_fail_no_include.cpp:3:17: error: 'regex_search' is not a member of 'std'
3 | return std::regex_search("aaa", re) ? 0 : 1;
| ^~~~~~~~~~~~
p1_fail_no_include.cpp:3:37: error: 're' was not declared in this scope
3 | return std::regex_search("aaa", re) ? 0 : 1;
| ^~
Read the errors' subjects: every name from the header fails the same way - regex is not a member of std, and neither is regex_search, because both live behind the same missing include. The language mode here is already C++11, so the mode is not the problem in this shape; the missing header is. The last error, 're' was not declared in this scope, is fallout: the declaration failed, so the variable does not exist where it is used. And note what is absent: no note under either error names <regex> or suggests the include - with this name, GCC leaves the remedy unstated, and knowing that <regex> is the header is the fix.
The same missing include at namespace scope is worded differently. This program declares the regex as a file-scope variable, still with no header:
std::regex g_re("a+");
int main() {
return std::regex_search("aaa", g_re) ? 0 : 1;
}
$ g++ -std=c++11 p1b_fail_scope.cpp -o p1b_fail_scope
p1b_fail_scope.cpp:1:6: error: 'regex' in namespace 'std' does not name a type
1 | std::regex g_re("a+");
| ^~~~~
p1b_fail_scope.cpp: In function 'int main()':
p1b_fail_scope.cpp:4:17: error: 'regex_search' is not a member of 'std'
4 | return std::regex_search("aaa", g_re) ? 0 : 1;
| ^~~~~~~~~~~~
p1b_fail_scope.cpp:4:37: error: 'g_re' was not declared in this scope
4 | return std::regex_search("aaa", g_re) ? 0 : 1;
| ^~~~
Same fault, different first line: where the name is used as a type outside a function, GCC says it does not name a type; where it is used inside a function, GCC says it is not a member of std. Recognize either wording as the missing-include shape - and expect no missing-header note under either one.
The second failure: <regex> included, compiled as C++98
The same use of the library with the header included still fails when the language mode is older than the library - and here the first diagnostic is not the title error at all, but a refusal from inside the library headers:
#include <regex>
#include <iostream>
#include <string>
int main() {
std::regex re("a+");
std::string text = "aaa";
std::cout << (std::regex_search(text, re) ? "match" : "no match") << "\n";
return 0;
}
$ g++ -std=c++98 p2_included.cpp -o p2_cpp98
In file included from /usr/include/c++/13/regex:37,
from p2_included.cpp:1:
/usr/include/c++/13/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
32 | #error This file requires compiler and library support \
| ^~~~~
p2_included.cpp: In function 'int main()':
p2_included.cpp:6:10: error: 'regex' is not a member of 'std'
6 | std::regex re("a+");
| ^~~~~
p2_included.cpp:8:24: error: 'regex_search' is not a member of 'std'
8 | std::cout << (std::regex_search(text, re) ? "match" : "no match") << "\n";
| ^~~~~~~~~~~~
p2_included.cpp:8:43: error: 're' was not declared in this scope
8 | std::cout << (std::regex_search(text, re) ? "match" : "no match") << "\n";
| ^~
The #error is the tell: it comes from the library's own headers, names the 2011 standard as the requirement, and names the remedy - enable -std=c++11 or -std=gnu++11. The include is present and correct, and adding it again changes nothing; the mode is the fault. The not-a-member errors still follow underneath, because with the header's contents withheld the names are absent exactly as in the missing-include shape - which is why reading only the later errors misleads: scroll to the first diagnostic, and if it is this #error, the compile line is what to fix. At -std=c++03 this exact program fails identically, line for line - same #error, same not-a-member errors - so both pre-C++11 modes produce this one shape. Raise the mode and the same file compiles unchanged: at -std=c++11 it compiles and prints match.
The header sweep: no header rescues in the probed set
A missing standard name is sometimes rescued by accident, when another header happens to include the one that declares it. For std::regex the sweep finds no rescue at all. Each sweep program uses the regex with one candidate header as its only include, and no other header - a second include would be its own possible rescue and would spoil the measurement. This program, with <string> in that position and still no <regex>, fails with the same first error as the missing-include shape:
#include <string>
int main() {
std::regex re("a+");
return std::regex_search("aaa", re) ? 0 : 1;
}
$ g++ -std=c++11 sweep_string.cpp -o sweep_string
sweep_string.cpp: In function 'int main()':
sweep_string.cpp:4:10: error: 'regex' is not a member of 'std'
4 | std::regex re("a+");
| ^~~~~
sweep_string.cpp:5:17: error: 'regex_search' is not a member of 'std'
5 | return std::regex_search("aaa", re) ? 0 : 1;
| ^~~~~~~~~~~~
sweep_string.cpp:5:37: error: 're' was not declared in this scope
5 | return std::regex_search("aaa", re) ? 0 : 1;
| ^~
<string> is the plausible candidate - the fix program searches inside a std::string, and std::smatch is built on string iterators - and it still does not bring the regex declarations in. The same program was compiled with each of <iostream>, <vector>, <algorithm>, <string_view> and <utility> as the only candidate header, at C++11: every one fails with the same first error, 'regex' is not a member of 'std'. So of the six headers probed in place of <regex>, none rescues this program - in this library, the regex declarations arrive only with <regex> itself, among the headers probed. That makes the repair simpler to state than for names with lucky neighbors: include <regex> directly in every file that names std::regex, because no other include in the sweep above stands in for it.
The fix: include <regex> and compile at C++11 or later
Both halves together - the header included, the mode at C++11 - and the library is fully usable. This program searches a string for the first run of digits, captures it in a group, and prints the whole match and the group through a std::smatch:
#include <regex>
#include <iostream>
#include <string>
int main() {
std::string text = "order 1234 shipped";
std::regex re("([0-9]+)");
std::smatch m;
if (std::regex_search(text, m, re)) {
std::cout << "matched " << m.str(0) << "\n";
std::cout << "group " << m.str(1) << "\n";
}
return 0;
}
$ g++ -std=c++11 p4_fix.cpp -o p4_fix
$ ./p4_fix
matched 1234
group 1234
Every name that failed before now resolves: std::regex_search - a free function from the header, not a member of the regex - finds the first place the pattern matches inside text and fills the std::smatch, and str reads the results back out: m.str(0) is the entire matched expression, and m.str(1) is the first capture group, the parenthesized part of the pattern. Here the pattern is one group around the whole match, so the two lines print the same digits - the number the search was for, extracted from the surrounding text. The output is fully determined by the input text and the pattern, so any other output means the program, not the machine, differs.
Neighboring error: a member std::regex does not have
Once <regex> is included and the mode is right, a wrong member name produces a different error on a different subject. Matching in this library is done by free functions - std::regex_search, std::regex_match, std::regex_replace - not by a member of the regex object, so asking the object to match reads naturally and fails like this:
#include <regex>
#include <iostream>
#include <string>
int main() {
std::regex re("a+");
std::string text = "aaa";
std::cout << (re.match(text) ? "match" : "no match") << "\n";
return 0;
}
$ g++ -std=c++11 p5_neighbour_no_member.cpp -o p5_neighbour_no_member
p5_neighbour_no_member.cpp: In function 'int main()':
p5_neighbour_no_member.cpp:8:22: error: 'std::__cxx11::regex' {aka 'class std::__cxx11::basic_regex<char>'} has no member named 'match'
8 | std::cout << (re.match(text) ? "match" : "no match") << "\n";
| ^~~~~
The contrast separates the two errors cleanly. This error names the class - std::basic_regex<char>, spelled out through the regex typedef - and the missing member, which proves the type was found: the include and the mode are already right, and only the member name is wrong, because the operation is not a member at all. The title error names regex as not a member of std, because the type was never there to be used. The repair here is to move the call out of the object: std::regex_search(text, re) searches with the same regex and the same text, as the fix program above does - or std::regex_match when the pattern must cover the whole string, not just a part of it.
Neighboring error: an invalid pattern throws at run time
The other neighboring fault is not a compile error in the first place. A pattern string is only read when the std::regex object is constructed, at run time, so a malformed pattern compiles cleanly and fails when the program runs - as a thrown std::regex_error, which this program catches and reports:
#include <regex>
#include <iostream>
int main() {
try {
std::regex re("a(");
std::cout << "no error\n";
} catch (const std::regex_error& e) {
std::cout << "regex_error: " << e.what() << "\n";
}
return 0;
}
$ g++ -std=c++11 p6_neighbour_regex_error.cpp -o p6_neighbour_regex_error
$ ./p6_neighbour_regex_error
regex_error: Mismatched '(' and ')' in regular expression
Nothing in this outcome says a name is missing - the compile produced no error at all, and the failure arrived while the program ran, carrying the pattern's own complaint: the ( in "a(" is never closed. Uncaught, the same throw ends the program with a termination message instead of this printed line, which is why a regex built from text the program does not control - a pattern typed by a user, read from a file or received as an argument - belongs inside a try block with a std::regex_error handler. The exact wording of the message is the library's own; the part to recognize is the exception type and its timing. When a regex program compiles and then dies at the construction line, read it as a pattern fault in the data, not as an include or mode problem - no header change fixes a malformed pattern.
Decision summary
- The first errors are
'regex' is not a member of 'std'and'regex_search' is not a member of 'std', with no#errorabove them and no note under them: the header is missing - add#include <regex>; every later error in the cascade traces to the absent names. - The first error is
'regex' in namespace 'std' does not name a type: the same missing include, read at namespace scope where the name is used as a type - add<regex>the same way. - The first diagnostic is a
#errorfrom inside the library headers saying the file requires compiler and library support for the ISO C++ 2011 standard, and the file already includes<regex>: the language mode is older than C++11 - the compile line shows-std=c++98,-std=c++03or an equivalent setting; compile at-std=c++11or later instead of changing code. The not-a-member errors underneath it are the same shape's fallout, and this shape appears at both probed older modes with identical wording. - The program includes some other header that sounds related -
<string>,<iostream>,<vector>,<algorithm>,<string_view>or<utility>: none of them declaresstd::regex; the probed sweep found no rescue from any of the six, so add<regex>itself. - The error instead names the class, as in
has no member named 'match'onstd::basic_regex<char>: the type was found and the member does not exist on a regex - matching is a free function; callstd::regex_searchorstd::regex_matchwith the text and the regex as arguments. - The program compiles, then fails at the line constructing the
std::regex, throwingstd::regex_error: the pattern string is malformed - fix the pattern, and keep construction of patterns that come from outside the program inside atryblock.
Checklist
- The file includes
<regex>itself - not a neighboring header in its place; in the sweep above, no probed header,<string>and<iostream>included, declaresstd::regexin its stead. - The compile line, or the build system's language standard setting, is C++11 or later; a pre-C++11 mode makes the library headers stop with a
#errorrequiring the ISO C++ 2011 standard, and the not-a-member errors follow it. - The first diagnostic is read, not just the title error: a
#errorabout the 2011 standard at the top means the mode is too old, while the not-a-member error standing alone - with no note under it - means the include is missing; the error line by itself cannot tell the two shapes apart, because both produce it. - The first error is read before the cascade: the
'was not declared in this scope'errors about the variable that failed to declare disappear when the name error at the top is fixed. - Matching is called as a free function -
std::regex_searchfor a match anywhere in the text,std::regex_matchfor the whole text - never as a member of the regex object. - A
std::regexbuilt from a pattern the program does not control is constructed inside atryblock, with a handler forstd::regex_error. - The fixed program compiles at C++11, runs, and prints
matched 1234andgroup 1234, in order.
Related guides
- 'std::string_view' Is Not a Member of 'std' Fix - the same missing-name fault on the string view type, with two contrasts worth seeing side by side: GCC attaches a note there naming the header to add, which it does not do for
std::regex, and<string_view>in an older mode leaves the not-a-member error as the only diagnosis, where<regex>stops with a#errorof the library's own first. - 'std::chrono' Is Not a Member of 'std' Fix - the same fault on the chrono library, and the same older-mode shape as here: at a pre-C++11 mode its header also stops with a
#errorfrom inside the library headers before the name errors follow. - 'std::filesystem' Is Not a Member of 'std' Fix - the same missing-name fault on the filesystem library:
std::filesystemis declared by<filesystem>, and because that library is C++17, it fails in both shapes covered here - missing include, and a mode that is too old for it.
Sources
- cppreference.com, std::basic_regex — the type at the center of the error: the class template
basic_regexsince C++11, a general framework for holding regular expressions, withstd::regexas itschartypedef - and a member list of construction, assignment,mark_count,flags, locale handling andswap, with no member that runs a match, which is the boundary the neighboring-error section draws. - cppreference.com, Standard library header
(C++11) — what the header declares: theregex_errorclass, thebasic_regexclass template and itsregexandwregextypedefs, thesub_matchandmatch_resultstemplates with thesmatchtypedef, and theregex_match,regex_searchandregex_replacefunction templates, all in namespacestd- the declarations a file gets by including<regex>itself — the sweep above found no probed neighbor that brings them in. - cppreference.com, std::match_results::str — the accessor the fix program uses:
strsince C++11, returning a string representing the indicated sub-match - the entire matched expression forstr(0), and the nth sub-match forstr(n)- the operation that turns thestd::smatchthe search fills into the two lines the fix prints.
Keep Exploring
Seven tips and seven puzzles so far, each one a short runnable example with the fix explained.
Paste an AddressSanitizer, UBSan, or ThreadSanitizer report and get the bug, the file and line, and the fix \u2014 in plain language.
C++20 and C++23 features, migration paths, and measured trade-offs.
The full C++ hub \u2014 language basics, the standard library, coroutines, and measured comparisons.
Paste a [Service] section and get a plain-language verdict on every sandboxing setting it finds.