📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)

 
(同じ利用者による、間の1版が非表示)
194行目: 194行目:
     try {
     try {
       htmlTagExtraction();
       htmlTagExtraction();
    }
    catch (const std::exception &e) {
      std::cerr << "Main function error: " << e.what() << std::endl;
      return -1;
    }
    return 0;
}
</syntaxhighlight>
<br><br>
== 正規表現の使用例 : URLの解析 ==
以下の例では、プロトコル、ドメイン、ポート番号、パス等の各要素を個別に抽出している。<br>
<br>
<syntaxhighlight lang="c++">
#include <iostream>
#include <regex>
#include <string>
void urlParsing()
{
    try {
      std::string url = "https://www.example.com:8080/path?param=value";
      std::regex pattern(R"(^(https?):\/\/([^:\/\s]+)(?::(\d+))?(\/[^\s]*)?$)");
      std::smatch matches;
      if (std::regex_match(url, matches, pattern)) {
          std::cout << "Protocol: " << matches[1] << std::endl;
          std::cout << "Domain: " << matches[2] << std::endl;
          std::cout << "Port: " << (matches[3].matched ? matches[3] : "default") << std::endl;
          std::cout << "Path: " << (matches[4].matched ? matches[4] : "/") << std::endl;
      }
      else {
          std::cerr << "Invalid URL format\n";
      }
    }
    catch (const std::regex_error &e) {
      std::cerr << "Regex error in URL parsing: " << e.what() << std::endl;
      std::cerr << "Code: " << e.code() << std::endl;
    }
    catch (const std::out_of_range &e) {
      std::cerr << "Out of range error in URL parsing: " << e.what() << std::endl;
    }
    catch (const std::exception &e) {
      std::cerr << "Error in URL parsing: " << e.what() << std::endl;
    }
}
int main()
{
    try {
      urlParsing();
    }
    catch (const std::exception &e) {
      std::cerr << "Main function error: " << e.what() << std::endl;
      return -1;
    }
    return 0;
}
</syntaxhighlight>
<br><br>
== 正規表現の使用例 : パスワードの検証 ==
以下の例では、大文字、小文字、数字、特殊文字を含む一般的なパスワードポリシーを実装している。<br>
<br>
<syntaxhighlight lang="c++">
#include <iostream>
#include <regex>
#include <string>
void passwordValidation()
{
    try {
      std::string password = "Password123!";
      std::regex pattern(R"(^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[!@#$%^&*])[A-Za-z\d!@#$%^&*]{8,})$)");
      if (std::regex_match(password, pattern)) {
          std::cout << "Valid password" << std::endl;
      }
      else {
          std::cout << "Invalid password. Password must contain:\n"
                    << "- At least one uppercase letter\n"
                    << "- At least one lowercase letter\n"
                    << "- At least one digit\n"
                    << "- At least one special character (!@#$%^&*)\n"
                    << "- Minimum length of 8 characters\n";
      }
    }
    catch (const std::regex_error &e) {
      std::cerr << "Regex error in password validation: " << e.what() << "\nCode: " << e.code() << std::endl;
      // パターンが複雑な場合の特別なハンドリング
      if (e.code() == std::regex_constants::error_complexity) {
          std::cerr << "Password pattern is too complex. Simplifying validation..." << std::endl;
          // 必要に応じてフォールバックの検証ロジックを実装
      }
    }
    catch (const std::exception &e) {
      std::cerr << "Error in password validation: " << e.what() << std::endl;
    }
}
int main()
{
    try {
      passwordValidation();
    }
    catch (const std::exception &e) {
      std::cerr << "Main function error: " << e.what() << std::endl;
      return -1;
    }
    return 0;
}
</syntaxhighlight>
<br><br>
== 正規表現の使用例 : IPアドレスの検証 ==
以下の例では、各オクテットの範囲チェックを含む厳密な検証を行っている。<br>
<br>
<syntaxhighlight lang="c++">
#include <iostream>
#include <regex>
#include <string>
void ipAddressValidation()
{
    try {
      std::string ip = "192.168.1.1";
      std::regex pattern(R"(^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$)");
      if (std::regex_match(ip, pattern)) {
          std::cout << "Valid IP address" << std::endl;
          // オプション: IPアドレスの種類をチェック
          std::smatch matches;
          std::regex privateIpPattern(R"(^(10\.|172\.(1[6-9]|2[0-9]|3[01])\.|192\.168\.).*)");
          if (std::regex_match(ip, matches, privateIpPattern)) {
            std::cout << "Note: This is a private IP address" << std::endl;
          }
      }
      else {
          std::cerr << "Invalid IP address format" << std::endl;
      }
    }
    catch (const std::regex_error &e) {
      std::cerr << "Regex error in IP validation: " << e.what() << std::endl;
      std::cerr << "Code: " << e.code() << std::endl;
    }
    catch (const std::exception &e) {
      std::cerr << "Error in IP validation: " << e.what() << std::endl;
    }
}
int main()
{
    try {
      ipAddressValidation();
    }
    catch (const std::exception &e) {
      std::cerr << "Main function error: " << e.what() << std::endl;
      return -1;
    }
    return 0;
}
</syntaxhighlight>
<br><br>
== 正規表現の使用例 : ログファイルの解析 ==
以下の例では、タイムスタンプ、ログレベル、メッセージを分離して抽出している。<br>
<br>
<syntaxhighlight lang="c++">
#include <iostream>
#include <regex>
#include <string>
void logFileAnalysis()
{
    try {
      std::string logLine = "[2024-01-01 12:34:56] ERROR: Database connection failed";
      std::regex pattern(R"(\[(.*?)\] (\w+): (.*))");
      std::smatch matches;
      if (std::regex_match(logLine, matches, pattern)) {
          if (matches.size() < 4) {
            throw std::runtime_error("Invalid log format: insufficient capture groups");
          }
          std::cout << "Timestamp: " << matches[1] << std::endl;
          std::cout << "Level: " << matches[2] << std::endl;
          std::cout << "Message: " << matches[3] << std::endl;
          // ログレベルの検証
          std::string level = matches[2];
          if (level != "INFO" && level != "WARNING" && level != "ERROR" && level != "DEBUG") {
            std::cerr << "Warning: Unknown log level " << level << std::endl;
          }
      }
      else {
          std::cerr << "Invalid log format" << std::endl;
      }
    }
    catch (const std::regex_error &e) {
      std::cerr << "Regex error in log analysis: " << e.what() << std::endl;
      std::cerr << "Code: " << e.code() << std::endl;
    }
    catch (const std::runtime_error &e) {
      std::cerr << "Runtime error in log analysis: " << e.what() << std::endl;
    }
    catch (const std::exception &e) {
      std::cerr << "Error in log analysis: " << e.what() << std::endl;
    }
}
int main()
{
    try {
      logFileAnalysis();
    }
    catch (const std::exception &e) {
      std::cerr << "Main function error: " << e.what() << std::endl;
      return -1;
    }
    return 0;
}
</syntaxhighlight>
<br><br>
== 正規表現の使用例 : 日付形式の変換 ==
以下の例では、regex_replaceメソッドを使用して日付形式を変更している。<br>
<br>
<syntaxhighlight lang="c++">
#include <iostream>
#include <regex>
#include <string>
void textReplacement()
{
    try {
      std::string text = "The date is 2023/12/25 and 2024/01/01";
      std::regex pattern(R"((\d{4})/(\d{2})/(\d{2}))");
      std::string result = std::regex_replace(text, pattern, "$3-$2-$1");
      std::cout << "Transformed: " << result << std::endl;
      // 置換が行われたか確認
      if (result == text) {
          std::cerr << "Warning: No dates were transformed" << std::endl;
      }
    }
    catch (const std::regex_error &e) {
      std::cerr << "Regex error in date transformation: " << e.what() << std::endl;
      std::cerr << "Code: " << e.code() << std::endl;
    }
    catch (const std::exception &e) {
      std::cerr << "Error in date transformation: " << e.what() << std::endl;
    }
}
int main()
{
    try {
      textReplacement();
     }
     }
     catch (const std::exception &e) {
     catch (const std::exception &e) {