|  | 
 
| MFC中利用正则表达式判断字符串是否为手机号,QQ号 在MFC中,可以使用Windows API提供的正则表达式功能来判断字符串是否为手机号或QQ号。以下是如何实现这一点的示例。
 
 ### 1. 引入必要的头文件
 
 在使用正则表达式之前,确保在项目中引入必要的头文件:
 
 
 复制代码```cpp
#include <regex>
#include <string>
```
 
 ### 2. 判断手机号
 
 中国手机号的正则表达式通常为:`^1[3-9]\d{9}$`。这个表达式的意思是:
 - 以1开头
 - 第二位是3到9之间的数字
 - 后面跟着9个数字
 
 ### 3. 判断QQ号
 
 QQ号的正则表达式可以是:`^[1-9][0-9]{4,10}$`。这个表达式的意思是:
 - 以1到9之间的数字开头
 - 后面跟着4到10个数字
 
 ### 4. 示例代码
 
 以下是一个完整的示例,展示如何在MFC中使用正则表达式判断字符串是否为手机号或QQ号:
 
 复制代码
```cpp
#include <afxwin.h> // MFC核心和标准组件
#include <regex>
#include <string>
class CMyApp : public CWinApp {
public:
    virtual BOOL InitInstance() {
        CString phone = _T("13812345678"); // 示例手机号
        CString qq = _T("123456"); // 示例QQ号
        if (IsValidPhone(phone)) {
            AfxMessageBox(_T("手机号有效"));
        } else {
            AfxMessageBox(_T("手机号无效"));
        }
        if (IsValidQQ(qq)) {
            AfxMessageBox(_T("QQ号有效"));
        } else {
            AfxMessageBox(_T("QQ号无效"));
        }
        return FALSE; // 结束应用程序
    }
    bool IsValidPhone(const CString& phone) {
        std::string phoneStr = CT2A(phone); // CString 转 std::string
        std::regex phoneRegex(R"(^1[3-9]\d{9}$)"); // 手机号正则表达式
        return std::regex_match(phoneStr, phoneRegex);
    }
    bool IsValidQQ(const CString& qq) {
        std::string qqStr = CT2A(qq); // CString 转 std::string
        std::regex qqRegex(R"(^[1-9][0-9]{4,10}$)"); // QQ号正则表达式
        return std::regex_match(qqStr, qqRegex);
    }
};
CMyApp theApp;
```
 
 ### 5. 说明
 
 - `CT2A` 是MFC提供的一个宏,用于将 `CString` 转换为 `std::string`。在这个例子中,我们将 `CString` 转换为 `std::string`,以便与标准库的正则表达式一起使用。
 - `std::regex_match` 函数用于检查给定字符串是否与正则表达式匹配。
 
 ### 总结
 
 通过以上代码,你可以在MFC中利用正则表达式判断字符串是否为有效的手机号或QQ号。
 
 
 | 
 |