hui zhang
hui zhang

Reputation: 123

std::regex construct throw exception

search line

km.key.AB_ABCDEFG

I want to Extract 'AB'

I use std::regex

string pattern("^km\\.key\\.([A-Z]{2})_");    //this throw a exception

regex reg(pattern, std::regex::extended );
smatch sm;
if(regex_match(line,sm,reg))
{
    key_type = sm[1];
    return 0;   
}

But I am sure this regex is right.

std::regex is so hard to use?

Please help.

Upvotes: 0

Views: 246

Answers (1)

Rimas
Rimas

Reputation: 6024

It's look likes compiler bug (one mentioned for gcc: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52719). Try on another compiler.

Another issue is with your regular expression - it must match entire string, look at documentation:

Note that regex_match will only successfully match a regular expression to an entire character sequence, whereas std::regex_search will successfully match subsequences.

This must work: ^km\\.key\\.([A-Z]{2})_.*

Upvotes: 1

Related Questions