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

履歴 編集

function
<regex>

std::basic_regex::swap(C++11)

void swap(basic_regex& e);

概要

正規表現オブジェクトを交換する。

効果

*thise の 2 つの正規表現オブジェクトの内容を交換する。

戻り値

なし

#include <iostream>
#include <regex>

void print(const std::regex& re)
{
  const char s[] = " abc 123 def ";
  std::cmatch m;
  std::regex_search(s, m, re);
  std::cout << "ready:" << std::boolalpha << m.ready() << std::endl;
  if (m.ready()) {
    std::cout << "prefix:'" << m.prefix() << '\'' << std::endl;
    for (std::size_t i = 0, n = m.size(); i < n; ++i) {
      std::cout << i << ":'" << m.str(i) << "', position = " << m.position(i) << std::endl;
    }
    std::cout << "suffix:'" << m.suffix() << '\'' << std::endl;
  }
  std::cout << std::endl;
}

int main()
{
  std::regex re1("\\w+");
  std::regex re2("\\d+");

  // swap
  print(re1);
  print(re2);

  re1.swap(re2);

  // swap
  print(re1);
  print(re2);
}

出力

ready:true
prefix:' '
0:'abc', position = 1
suffix:' 123 def '

ready:true
prefix:' abc '
0:'123', position = 5
suffix:' def '

ready:true
prefix:' abc '
0:'123', position = 5
suffix:' def '

ready:true
prefix:' '
0:'abc', position = 1
suffix:' 123 def '

バージョン

言語

  • C++11

処理系