user4881368
user4881368

Reputation:

getting error using regex in C++

Given input has 4 lines and I am supposed to find how many lines have word hacker

4
I love #hacker
I just scored 27 points in the Picking Cards challenge on #Hacker
I just signed up for summer cup @hacker
interesting talk by hari, co-founder of hacker

The answer is 4 but I get it as 0.

int main() {
    int count = 0,t;
    cin >> t;
    string s;
    bool ans;
    while(t--){
        cin >> s;
        smatch sm;
        regex rgx("hacker",regex_constants::icase);
        ans = regex_match(s,sm,rgx);
        if(ans){
            count += 1;
        }         
    }
    cout << ans << endl;
    return 0;
}

Upvotes: 0

Views: 156

Answers (4)

jxh
jxh

Reputation: 70392

It looks like the first word is supposed to be the number of lines of input. But, even though it seems you want to process four lines of input, the input says 3.Question has since been edited.

You are not reading lines, but strings, which translates into individual words. Use getline() to get a line of input.

while(t--){
    std::getline(std::cin, s);
    //...

Your regular expression is ill-formed. It will only match if the line consists only of the word "hacker". You want to see if hacker is in the line, so make allow your pattern to match the rest of the line around the word "hacker".

    regex rgx(".*hacker.*",regex_constants::icase);

When you emit your answer, it seems you want to emit count, not ans.

DEMO

Upvotes: 1

Steephen
Steephen

Reputation: 15824

If you change your regex as follows, you will get expected result:

regex rgx("(.*)hacker(.*)",regex_constants::icase);

So it is basically comparing for the match in whole string.

Otherwise you have to use std::regex_search in place of std::regex_match

ans = regex_search(s,sm,rgx);

Demo: http://coliru.stacked-crooked.com/a/f28c2e4b315f6f0a

Upvotes: 1

Freszone
Freszone

Reputation: 49

You should use std::getline instead to read a string (containing whitespaces). Also, you should use std::regex_search to search for a 'partial' match (std::regex_match will only match when the regex matches the whole string).

Here's your code a little modified:

#include <regex>
#include <iostream>
#include <string>

int main() {
    int count = 0,t;
    std::cin >> t;
    std::string s;
    std::smatch sm;
    std::regex rgx("hacker", std::regex_constants::icase);

    for(int i = 0; i < t; ++i)
    {
        std::getline(std::cin, s);
        while(std::regex_search(s, sm, rgx))
        {  
            ++count;
            s = sm.suffix().str();
        }
    }
    std::cout << count << std::endl;
    return 0;
}

Upvotes: 1

kgwong
kgwong

Reputation: 121

  1. Your while loop only runs t times, and every time it only reads one word. So your program right now will only read the first three words and then terminate.

  2. You're only matching the whole word. In the case of #hacker an @hacker, there will be no match.

  3. I believe you want to cout count instead of ans at the end.

Upvotes: 1

Related Questions