How to validate a 10-digit phone number with hyphens (e.g. 333-555-1234) using RegEx in C++

1 Answer

0 votes
#include <iostream>
#include <string>
#include <regex>

int main() {
    std::string phone_number = "333-555-1234";
    std::regex phone_regex(R"(\d{3}-\d{3}-\d{4})");

    if (std::regex_match(phone_number, phone_regex)) {
        std::cout << "Valid phone number format" << std::endl;
    } else {
        std::cout << "Invalid phone number format" << std::endl;
    }
}



  
/*
run:
  
Valid phone number format
  
*/

 



answered Feb 21, 2025 by avibootz
...