Match Results Length In C++ STL - C++ Programming Tutorial
C++ Course / STL Basics / Match Results Length In C++ STL

Match Results Length In C++ STL

BLUF: Mastering Match Results Length In C++ STL is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Match Results Length In C++ STL

C++ is renowned for its efficiency. Learn how Match Results Length In C++ STL enables low-level control and high-performance computing in the tutorial below.

The C++ Standard Library offers Regular Expressions features that involve the std::matchresults template class. This template is employed in combination with regular expressions to store the result of a regex match. To determine the length of the matched text for a specific submatch, you can utilize the length method present in std::matchresults.

The length member function within the C++ STL is utilized to determine the size of a matched substring associated with the std::match_results class.

Syntax:

The following is the syntax for using length:

Example

size_t length() const;

Return type:

The data type size_t signifies an unsigned integer that indicates the maximum size of an object that can be accommodated within the system's memory allocation structure.

Parameters:

There are zero parameters needed for the length function to function properly.

Usage:

To determine the total length of the matched text, the length method is typically called on a std::match_results object instance.

Alternatively, it can be employed to retrieve the size of a specific submatch when paired with an index.

1. Regular Expressions in C++

When it comes to searching for patterns within strings, regular expressions prove to be a useful tool.

A header in the C++ Standard Library provides functions and classes for working with regular expressions.

2. std::match_results Template Class

One of the features within the Regular Expressions module of the C++ Standard Library is the std::match_results class template .

It's a template class that retains the results of a regular expression match and information about matching subexpressions.

3. length Member Function

The std::match_results module includes a member function called length.

It is utilized to retrieve the length of text that corresponds to a particular submatch identified by its index.

4. Use Cases

It is beneficial to have the ability to ascertain the length of the matched text when further processing or validation is necessary.

It can be utilized in scenarios where you need to extract and manipulate substrings that adhere to regular expressions.

5. Caution

Exercise care when utilizing the length function for submatches to avoid encountering undefined behavior and verify the validity of the submatch index.

Example:

Let's consider a scenario to demonstrate the length function of match_result in C++.

Example

#include <iostream>
#include <regex>
int main() 
{    
// Input string
    std::string input = "The quick brown fox jumps over the lazy dog.";
    // Regular expression pattern to match words
    std::regex wordPattern("\\b\\w+\\b");
    // std::smatch to store the match results
    std::smatch matches;
    // Perform regex_search to find the first match
    if (std::regex_search(input, matches, wordPattern))
    {
        // Display the entire match and its length
        std::cout << "Entire match: " << matches.str() << std::endl;
        std::cout << "Length of the entire match: " << matches.length() << std::endl;
        // Display individual submatches and their lengths
        for (size_t i = 0; i < matches.size(); ++i)
        {
            std::cout << "Submatch " << i << ": " << matches[i].str() << std::endl;
            std::cout << "Length of submatch " << i << ": " << matches.length(i) << std::endl;
        }
    }
    return 0;
}

Output:

Output

Entire match: The
Length of the entire match: 3
Submatch 0: The
Length of submatch 0: 3

Explanation:

  1. Include Headers

These headers in C++ are commonly used for managing regular expressions and handling input/output operations.

  1. Explain Regular Expression and Input String

An input string refers to a sequence of characters that contains the specific text to be located through a search operation.

WordPattern represents a regex pattern that matches words, utilizing escape sequences like \b to mark word boundaries and \w to represent word characters.

  1. Initialize std::smatch to store the matching results.

Declare a custom match_results instance, std::smatch, to hold the results of a regular expression match. This particular object is designed to contain details regarding the findings of a regex search.

  1. Execute the Regex_search operation.

Locate the wordPattern regular expression within the input string by employing the std::regex_search function. The matches object serves as a container for storing the results of the match.

  1. Present the complete match along with its corresponding length.

If a match is found, you can utilize matches.str and matches.length to display the entire matched sub-string along with its length.

  1. Viewing Specific Submatches and Their Respective Lengths

Afterward, by iterating over particular submatches (capturing groups), the script displays the size of each submatch by utilizing matches[i].matches.length(i) and str.

  1. Output: 0

Signals the operating system that the program has finished execution.

Conclusion:

In summary, this C++ program employs regular expressions to scan for words within a specified string. The scanning process utilizes std::regex_search, with the outcomes of the search saved in std::smatch. Subsequently, the program displays both the complete match and the specific details of each submatch, such as its length. This illustration showcases the utilization of the C++ <regex> functionalities for extracting and manipulating match data.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience