std::basic_...:basic_regex(3)
β¨ NAME β¨
okay so bestie, this is the std::basic_regex::basic_regex page from the C++ Standard Libary (their spelling, not mine π), a.k.a. std::basic_...:basic_regex(3). It's literally the constructor page for std::basic_regex::basic_regex β we're building regex girlies out of character sequences, and it's giving main character energy. π
πΈ Synopsis πΈ
Here's the full constructor lineup, exactly as the page has it:
basic_regex();
(1) (since C++11)
explicit basic_regex( const CharT* s,
flag_type f =
(2) (since C++11)
std::regex_constants::ECMAScript );
basic_regex( const CharT* s, std::size_t count,
(3) (since C++11)
flag_type f = std::regex_constants::ECMAScript );
basic_regex( const basic_regex& other );
(4) (since C++11)
basic_regex( basic_regex&& other ) noexcept;
(5) (since C++11)
template< class ST, class SA >
explicit basic_regex( const std::basic_string<CharT,ST,SA>& str,
(6) (since C++11)
flag_type f =
std::regex_constants::ECMAScript );
template< class ForwardIt >
basic_regex( ForwardIt first, ForwardIt last,
(7) (since C++11)
flag_type f = std::regex_constants::ECMAScript );
basic_regex( std::initializer_list<CharT> init,
(8) (since C++11)
flag_type f = std::regex_constants::ECMAScript );
Constructs a new regular expression from a sequence of characters interpreted according to the flags f. No cap, that's the whole vibe. π
- Default constructor. Constructs an empty regular expression which will match nothing.
- Constructs a regex from a null-terminated string
s. - Constructs a regex from a sequence of
countcharacters, pointed to bys. - Copy constructor. Constructs a regex by copying
other. - Move constructor. Constructs a regex with the contents of
otherusing move semantics. - Constructs a regex from a string
str. - Range constructor. Constructs the string with the contents of the range
[first, last). - Initializer list constructor. Constructs the string with the contents of the initializer list
init.
π¦ Parameters π¦
sβ pointer to a null-terminated string.countβ length of a character sequence used to initialize the regex.first,lastβ range of a character sequence used to initialize the regex.strβ abasic_stringused as a source used to initialize the regex.otherβ another regex to use as source to initialize the regex.initβ initializer list used to initialize the regex.fβ flags used to guide the interpretation of the character sequence as a regular expression.
π§ Type requirements π§
ForwardItmust meet the requirements ofLegacyForwardIterator.
π Exceptions π
- 1) May throw implementation-defined exceptions.
- 2,3)
std::regex_errorif the supplied regular expression is not valid. - 4) May throw implementation-defined exceptions.
- 6-8)
std::regex_errorif the supplied regular expression is not valid.
note: the page literally skips 5) β that's not me being flaky, it's just not there π
π₯ Example π₯
Run this code, bestie:
// Run this code
#include <iomanip>
#include <iostream>
#include <regex>
#include <string>
void match_and_print(const std::string& text, const std::regex& pattern)
{
std::sregex_iterator it(text.begin(), text.end(), pattern), it_end;
int count = 0;
for (; it != it_end; ++it)
{
const std::smatch& match = *it;
std::cout << ++count << ". " << std::quoted(match.str()) << '\n';
}
std::cout << (count ? "\n" : "no match found\n\n");
}
int main()
{
const std::string text = "Hello, World! 12345";
// Matches one or more digits
std::string pattern_text = "\\d+";
std::cout << "digits (" << pattern_text << "):\n";
auto pattern = std::regex(pattern_text);
match_and_print(text, pattern);
// Matches one or more characters split by space
pattern_text = "[^\\s]+";
std::cout << "words (" << pattern_text << "):\n";
pattern = std::regex(pattern_text);
match_and_print(text, pattern);
// Matches one or more characters split by space
pattern_text = "[a-zA-Z]+";
std::cout << "words without symbols and digits (" << pattern_text << "):\n";
pattern = std::regex(pattern_text);
match_and_print(text, pattern);
// Matches one non digits, non alphabet
pattern_text = "[^0-9A-Za-z]";
std::cout << "symbol (" << pattern_text << "):\n";
pattern = std::regex(pattern_text);
match_and_print(text, pattern);
// Matches one or more lowercase
pattern_text = "[a-z]+";
std::cout << "lowercase (" << pattern_text << "):\n";
pattern = std::regex(pattern_text);
match_and_print(text, pattern);
// Matches one or more lowercase with std::regex::icase flag
pattern_text = "[a-z]+";
std::cout << "lowercase with ignore case flag (" << pattern_text << "):\n";
pattern = std::regex(pattern_text, std::regex::icase);
match_and_print(text, pattern);
// Matches basic POSIX regular expression
pattern_text = "[[:digit:]]+";
std::cout << "basic POSIX regex (" << pattern_text << "):\n";
pattern = std::regex(pattern_text, std::regex::basic);
match_and_print(text, pattern);
// Matches extended POSIX regular expression
pattern_text = "[[:digit:]]+";
std::cout << "extended POSIX regex (" << pattern_text << "):\n";
pattern = std::regex(pattern_text, std::regex::extended);
match_and_print(text, pattern);
}
Output:
digits (\d+):
1. "12345"
words ([^\s]+):
1. "Hello,"
2. "World!"
3. "12345"
words without symbols and digits ([a-zA-Z]+):
1. "Hello"
2. "World"
symbol ([^0-9A-Za-z]):
1. ","
2. " "
3. "!"
4. " "
lowercase ([a-z]+):
1. "ello"
2. "orld"
lowercase with ignore case flag ([a-z]+):
1. "Hello"
2. "World"
basic POSIX regex ([[:digit:]]+):
no match found
extended POSIX regex ([[:digit:]]+):
1. "12345"
okay that's the whole page bestie π β source http://cppreference.com, version/date 2024.06.10, page std::basic_...:basic_regex(3). go forth and construct your regexes, mwah π
β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms