I would like to write a function that determines how many characters of a string (starting from the beginning) match a regular expression. The function signature would look something like this:
string::size_type matched_length(const string& s, const regex& r);
If all the characters in s up to the length of s match then s.size() is returned. If the first character cannot satisfy the regex the function would return zero. If some number of chars are potentially the beginnings of a string that could satisfy the regex that length is returned. This function would not indicate if s matches r or not - that is what regex_match() does.
Is this possible? I'm not having much luck extracting information from the c++ regex library on failed matches.
Thanks.
I did find this relevant discussion: Output the first character that causes mismatch with a regular expression however I think it is incorrect that a person can't determine this in general. The Qt library's QRegExp has this exact function and I've found it very useful when writing text entry widgets.