C++ Standard Library Regular Expression Operations
Regular expression syntax utilizes specific characters to define search patterns:
\: Escapes a special character*: Matches zero or more occurrences+: Matches one or more occurrences?: Matches zero or one occurrence{n,m}: Matches betweennandmoccurrences.: Matches any single character|: Logical OR between expressions[abc]: Matches any character in the set[^abc]: Matches any character not in the set[a-z]: Matches characters within the range
Implementing pattern matching in C++ involves three primary stages:
- Constructing the pattern object:
std::string source = "Locate the IP: 192.168.0.1";
std::string pattern_str = R"(\d+\.\d+\.\d+\.\d+)";
std::regex ip_regex(pattern_str);
- Executing the algorithm and capturing the outcome:
std::smatch captures;
std::regex_search(source, captures, ip_regex);
- Accessing the matched data:
std::cout << "Detected: " << captures[0] << std::endl;
Core Library Types
The <regex> header providse two fundamental template instantiations for pattern definitions:
using regex = basic_regex<char>;
using wregex = basic_regex<wchar_t>;
To store the outcomes of matching operations, the library offers several aliases based on match_results:
using cmatch = match_results<const char*>;
using wcmatch = match_results<const wchar_t*>;
using smatch = match_results<string::const_iterator>;
using wsmatch = match_results<wstring::const_iterator>;
Full Sequence Validation
std::regex_match evaluates whether the entire input sequence conforms exactly to the specified pattern.
#include <regex>
#include <iostream>
#include <vector>
int main() {
std::string decimal_rule(R"(^[-+]?([0-9]*\.[0-9]+|[0-9]+)$)");
std::regex validator(decimal_rule);
std::vector<std::string> inputs = {"42", "-3.14", "+0.5", "1e5", "abc"};
for (const auto& item : inputs) {
if (std::regex_match(item, validator)) {
std::cout << item << " is a valid numeric format.\n";
} else {
std::cout << item << " is invalid.\n";
}
}
return 0;
}
Partial Sequence Search
std::regex_search locates subsequences within the target text that satisfy the pattern, requiring no full-sequence conformity.
#include <iostream>
#include <string>
#include <regex>
#include <vector>
int main() {
std::vector<std::string> logs = {
"System error at 2023-10-05T14:30:00",
"User login on 2023-10-06T08:15:22",
"Routine check passed",
"Alert triggered 2023-10-07T23:59:59"
};... (rest of the JSON formatted markdown)
```
Wait, let me make sure the JSON is properly escaped. I'll write the JSON carefully.