Algoteka
Log in to submit your own samples!

Search with a Regex Pattern

Problem by oml1111
# Tech tags Title Creator Created date
1 0
2022-12-19 19:13
View all samples for this language (1 verified and 0 unverified)

STL solution | C++ |

By oml1111 |
0 likes

Does the matching using std::regex_search

Code

#include<regex>
#include<string>

int main() {
    std::string text = "Our coordinates are (a=123, b=5432), and we have some values too";
    
    std::regex pattern("a=(\\d+), b=(\\d+)");
    std::smatch match;
    
    if(std::regex_search(text, match, pattern)) {
        std::string a = match[1];  // acquires value "123"
        std::string b = match[2];  // acquires value "5432"
    }

    return 0;
}

Further reading

<regex> - cplusplus.com
ECMAScript syntax - cplusplus.com

References

classes
std::regex en.cppreference.com cplusplus.com
std::smatch en.cppreference.com cplusplus.com
std::string en.cppreference.com cplusplus.com
functions
std::regex_search en.cppreference.com cplusplus.com

Problem Description

Construct a regex pattern with some groups. Find a segment of some string that matches said pattern and fetch the parts that match the groups specified by the pattern.

View sample discussion (0 comments)
View problem discussion (0 comments)