Reputation: 477
I have a character stream, producing one char each step.
'1'
, final string '1'
'2'
, final string '12'
Each step I want to know if the produced final string (sum of every char produced) has possibility to be matched by regexp in the future.
Example regex: ^\d{5}
So the string '123'
is not matched by this exact regex, but if the character stream produces two more digits, on the fifth step it would match.
But the string 'a12'
is already not matched and not possible to be matched in the future.
My question is - is there a possibility to separate those two cases in regex matching engine? This looks like a FSM(finite state machine) task to check if the FSM has successful match, error or is in progress of its matching graph. I know that regex inside is FSM, but I don't know if it is possible to separate not matching case in two different cases as described above.
Upvotes: 1
Views: 70
Reputation: 12899
Following on from the comment, you could make use of boost::regex
and its boost::regex_constants::match_partial
flag...
boost::regex re("^(\\d{5})$");
boost::smatch caps;
std::string text = ...;
if (boost::regex_match(text, caps, re, boost::match_default | boost::match_partial) {
/*
* Matched successfully. Need to decide whether or not match is partial.
*/
if (caps[0].matched) {
/*
* Match is complete.
*/
} else {
/*
* Match is partial.
*/
}
}
Upvotes: 2