1

I'm simply calling

std::smatch m;
if (std::regex_search 
  (std::string (strT.GetString ()), 
  m,
  std::regex ("((\\d[\\s_\\-.]*){10,13})")))
{
  ...
}

I can't for the life of me figure out how to extract the matched values from m.

EVERY SINGLE page on the subject writes it to cout which is worthless to me. I just want to get what's been captured in a string, but no matter what I try it crashes with a "string iterators incompatible" error message.

OK so I tried a few more things and got annoyed at a lot more, most notably about how the same code worked in online testers but not on my computer. I've come down to this

std::string s (strT.GetString ()) ;
std::smatch m;
if (std::regex_search (
    s, 
    m,
    std::regex ("((\\d[\\s_\\-.]*){10,13})")))
{
    std::string v = m[ 0 ] ;
}

working, but this

std::smatch m;
if (std::regex_search (
    std::string (strT.GetString ()), 
    m,
    std::regex ("((\\d[\\s_\\-.]*){10,13})")))
{
    std::string v = m[ 0 ] ;
}

Not Working For Some Reason (with the incompatible string iterator error thingy). There's surely some trick to it. I'll let someone who knows explain it.

  • do you just want to know if string matches the regex or do you want to search items using a regex? – OznOg May 29 '19 at 18:15
  • Use `sregex_token_iterator` or `sregex_iterator`. Fix your regex, too, but that is not the current issue. – Wiktor Stribiżew May 29 '19 at 18:19
  • My pattern captures exactly what I need, thank you very much. What I don't get is how I can 'read' my captures in the `std::smatch m;` var. –  May 31 '19 at 08:39
  • @LightnessRacesinOrbit Seems likely. Can you write it in an answer so I'll accept it? Also would it work by placing a `GetBuffer()` instead of the `GetString()` ? While I can see the necessity I don't like using an entire variable for a single line. –  May 31 '19 at 15:24
  • Yeah I've decided that wasn't a useful dupe. Re-opened and answered. – Lightness Races in Orbit May 31 '19 at 15:45

1 Answers1

0

You are correct that you can just assign the match to a std::string; you don't have to use the stream insertion feature.

However, your third example crashes because std::smatch holds references/handles to positions in the original source data … which in your crashy case is the temporary strT.GetString() that went out of scope as soon as the regex was done (read here).

Your second example is correct.

I concede that the C++ regex implementation is not entirely intuitive at first glance.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055