0
  boost::regex re;
  re = "(\\d+)";
  boost::cmatch matches;
  if (boost::regex_search("hello 123 world", matches, re))
    {
    printf("Found %s\n", matches[1]); 
    }

結果:「世界123個発見」。「123」が欲しかっただけです。これはヌル終了の問題ですか、それとも regex_search の仕組みを誤解しているだけですか?

4

1 に答える 1

2

matches[1]そのように (タイプのオブジェクトsub_match<T>) を printfに渡すことはできません。printf は char ポインターを想定しているため、有用な結果が得られるという事実は当てにできません。代わりに次を使用します。

cout << "Found " << matches[1] << endl;

または、printf を使用する場合:

printf("Found %s\n", matches[1].str().c_str());

を使用して、結果で std::string オブジェクトを取得できますmatches[1].str()

于 2010-01-26T18:47:05.757 に答える