user5204184
user5204184

Reputation: 341

Reading white spaces, new lines and tabs with regex in C++

I am trying to capture white spaces \s, end of lines '\n', '\r', tabs '\t', but not having success.

Here's what I tried:

#include <iostream>
#include <regex>

int main()
{
std::regex s ("\\s(.*)");
std::regex n ("\\n(.*)");
std::regex r ("\\r(.*)");
std::regex t ("\\t(.*)");
const char str[]=" subject \rsubject \nsubject \tsubject";
std::cmatch cmS;
std::cmatch cmN;
std::cmatch cmR;
std::cmatch cmT;
 if (std::regex_match (str, cmS,s))
     for (unsigned i=0; i<cmS.size(); ++i) {
         std::cout << "[" << cmS[i] << "] ";
     }
 if (std::regex_match (str, cmN,n))
     for (unsigned i=0; i<cmN.size(); ++i) {
         std::cout << "[" << cmN[i] << "] ";
     }
 if (std::regex_match (str, cmR,r))
     for (unsigned i=0; i<cmR.size(); ++i) {
         std::cout << "[" << cmR[i] << "] ";
     }

 if (std::regex_match (str, cmT,t))
     for (unsigned i=0; i<cmT.size(); ++i) {
         std::cout << "[" << cmT[i] << "] ";
     }

return 0;
}

I also tried like this but didn't have any success either and my program was crashing:

if (std::regex_match ("subject subject", std::regex("\\s(sub)"),std::regex_constants::ECMAScript ))
    std::cout << "string literal matched\n";

if (std::regex_match ("subject subject", std::regex("\s(sub)"),std::regex_constants::ECMAScript ))
    std::cout << "string literal matched\n";

if (std::regex_match ("subject subject", std::regex("[[:s:]](sub)"),std::regex_constants::ECMAScript ))
    std::cout << "string literal matched\n";

I know there are some external classes like boost to do regex in C++, but my goal is to not use any external classes and dependencies for my program, so I need to do this inside C++ itself.

Upvotes: 2

Views: 4939

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627102

To enable the regex, you need to install GCC 4.9.0 or higher as in the former compile versions regex module did not work.

Next, you need to use regex_search rather than regex_match, as the latter requires a full string match, and you are looking for substrings.

See regex_match description:

The entire target sequence must match the regular expression for this function to return true (i.e., without any additional characters before or after the match). For a function that returns true when the match is only part of the sequence, see regex_search.

Upvotes: 3

james jelo4kul
james jelo4kul

Reputation: 829

If you only need to match the white space then i see no need for you adding (.*)

Try removing (.*) in each of your statement. Your new statement should now be like this

std::regex s ("\\s"); 
std::regex n ("\\n"); 
std::regex r ("\\r");
std::regex t ("\\t");

Upvotes: 1

Related Questions