最終更新日時(UTC):
が更新

履歴 編集

function
<regex>

std::sub_match::compare(C++11)

int compare(const sub_match& s) const;   // (1)

int compare(const string_type& s) const; // (2)

int compare(const value_type* s) const;  // (3)

概要

マッチした文字列を比較する。

戻り値

備考

(1) の形式でもマッチした文字列のみが比較され、マッチした位置は考慮されない。(例を参照)

#include <iostream>
#include <regex>
#include <string>

int main()
{
  const char s[] = "123 123";
  const std::regex re(R"((\d+) (\d+))");
  std::cmatch m;
  if (std::regex_search(s, m, re)) {
    std::csub_match sub1 = m[1];
    std::csub_match sub2 = m[2];
    std::cout << sub1.compare(sub2) << std::endl                // (1) の形式
              << sub1.compare(std::string("012")) << std::endl  // (2) の形式
              << sub1.compare("234") << std::endl;              // (3) の形式
  } else {
    std::cout << "not match" << std::endl;
  }
}

出力例

0
1
-1

注:比較結果が等しくない時の戻り値は符号のみが規定されているため、出力は上記とは異なる可能性がある。(basic_string::compare 参照)

バージョン

言語

  • C++11

処理系