0

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.

Houe
  • 51
  • 5
  • 1
    Can you please show some input/output examples? It'll help clarify the question. – cigien Jul 01 '21 at 04:13
  • I think if you have the results from regex_match you can just sort them in decreasing order attending to size, and then iterate over the results comparing them with the start of your string. you return the very first coincidence then, and if none of them coincide, then return 0. – robbinc91 Jul 01 '21 at 04:31
  • Can't you change the regex to include start of string and end of string symbols in the regular expression and then feed substrings of your string? Okay, not the most performant, but should be working? This approach could be optimized by a search strategy. – Sebastian Jul 01 '21 at 06:13
  • 1
    Related to [is-there-a-match-partial-in-c11-regular-expressions](https://stackoverflow.com/questions/7695891/is-there-a-match-partial-in-c11-regular-expressions). – Jarod42 Jul 01 '21 at 08:00
  • And boost has [partial_matches](https://www.boost.org/doc/libs/1_76_0/libs/regex/doc/html/boost_regex/partial_matches.html). – Jarod42 Jul 01 '21 at 08:01
  • Thanks Jarod42. This looks to be the answer. Its not available in the C++ version (at least not yet). Boost could be an alternative. – Houe Jul 01 '21 at 12:56

0 Answers0