A valuable function provided in the C++ Standard Library header is std::regex_search. This function is designed to search a given string for matches based on a specified regular expression pattern. Regular expressions are sequences of characters that define a specific pattern for searching. They are particularly handy for identifying patterns within strings.
When comparing a subsequence within the target sequence (referred to as the subject) with the regular expression rgx (referred to as the pattern), the comparison output is provided. The target sequence, depending on the version in use, may refer to either 's' or the character sequence located between the first and last positions.
Functionality:
Instead of necessitating a regular expression pattern to match the complete input string, std::regex_search seeks out the initial occurrence of the pattern within a specified input string.
Syntax:
It has the following syntax:
template <class BidirectionalIterator, class CharT, class Traits>
bool regex_search(BidirectionalIterator first, BidirectionalIterator last, std::basic_regex
===============================================================
<CharT, Traits> const& rex, std::match_results<BidirectionalIterator> &m,
std::regex_constants::match_flag_type flags = std::regex_constants::match_default);
Parameters:
- Input iterators specifying the range of the target string to search should come first and last.
- The object to match against in a regular expression is called rex.
- m: The location of the matched results in a std::match_results object.
- flags: Flags that can be used to modify how a search operates.
- If the regular expression pattern appears anywhere in the target string, the return value is true; otherwise, it is false.
- If a match is discovered, the matched outcomes are kept in the supplied std::match_results object.
Return value:
Example 1:
Let's consider a scenario to demonstrate the std::regex_search function in C++.
#include <iostream>
#include <regex>
#include <string>
int main() {
std::string target = "The quick brown fox jumps over the lazy dog";
std::regex pattern("quick (\\w+)");
std::smatch matches;
if (std::regex_search(target, matches, pattern)) {
std::cout << "Match found: " << matches.str(0) << std::endl;
std::cout << "Captured group: " << matches.str(1) << std::endl;
} else {
std::cout << "No match found." << std::endl;
}
return 0;
}
Output:
Match found: quick brown
Captured group: brown
Explanation:
In this instance, the program scans the target string for the specified pattern "quick (word)". When a match is detected, both the complete match and the text within the captured group are displayed. The std::regex_search function serves as a fundamental feature for pattern matching in C++, empowering developers to apply regular expressions for complex string operations.
Example 2:
Let's consider another instance to demonstrate the std::regex_search function in C++.
#include <iostream>
#include <regex>
#include <string>
int main() {
std::string target = "The quick brown fox jumps over the lazy dog";
std::regex pattern("\\b\\w{4}\\b"); // Matches 4-letter words
std::sregex_iterator it(target.begin(), target.end(), pattern);
std::sregex_iterator end;
std::cout << "Words with 4 letters found: ";
while (it != end) {
std::smatch match = *it;
std::cout << match.str() << " ";
++it;
}
std::cout << std::endl;
return 0;
}
Output:
Words with 4 letters found: over lazy
Explanation:
- There is a sentence in our target string target.
- Matching four-letter words is the regular expression pattern \\b\\w{4}\\b.
- For each match found in the target string, we iterate over it using std::sregex_iterator.
- We use std::smatch to extract and print the matched word for each match.
Example 3:
Let's consider another scenario to demonstrate the functionality of the std::regex_search function in C++.
#include <iostream>
#include <string>
#include <regex>
int main() {
std::string text("Find sub words such as subject, submarine, and subsequence.");
std::smatch match;
std::regex pattern("\\b(sub)(\\w*)");
std::cout << "Input text: " << text << std::endl;
std::cout << "Regular expression pattern: /\\b(sub)(\\w*)/" << std::endl;
std::cout << "Matching results:" << std::endl;
while (std::regex_search(text, match, pattern)) {
for (auto& m : match)
std::cout << m << " ";
std::cout << std::endl;
text = match.suffix().str();
}
return 0;
}
Output:
Input text: Find sub words such as subject, submarine, and subsequence.
Regular expression pattern: /\b(sub)(\w*)/
Matching results:
sub sub
subject sub ject
submarine sub marine
subsequence sub sequence
Conclusion:
In summary, the provided code illustrates the utilization of std::regexsearch in C++ for extracting specific patterns such as numerical values from a given text. Regular expressions offer a straightforward approach for defining intricate search criteria, empowering programmers with robust text processing capabilities. This implementation showcases a basic procedure involving std::smatch for extracting matched substrings, leveraging std::sregexiterator for iterating through the matches, and initializing the input text along with the regular expression pattern. Overall, std::regex_search serves as a valuable asset for tasks necessitating pattern matching and content extraction in text processing scenarios, allowing C++ developers to perform sophisticated string operations with efficiency.