一、需求
- 命名规则:0-9 a-z 不能反斜杠,长度限制(30)
- 1. Project命名规则
- 1. 可以包含:
- 1. 汉字,
- 2. 字母(区分大小写),
- 3. "_"(英文下划线),
- 4. “-”(英文破折线),
- 5. “.”(英文点);
- 7. 数字
- 2. 长度:30个字符;
二、实现
#include <iostream>
#include <regex>
#include <locale>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
std::wstring transformString2Wstring(const std::string &s)
{
setlocale(LC_CTYPE, "en_US.UTF-8");
const char *_Source = s.c_str();
size_t len = strlen(_Source) + 1;
size_t converted = 0;
wchar_t *wStr = new wchar_t[len];
mbstowcs_s(&converted, wStr, len, _Source, _TRUNCATE);
std::wstring result(wStr);
delete[] wStr;
return result;
}
int main() {
std::wstring pattern{L"^[\u4E00-\u9FA5A-Za-z0-9][\u4E00-\u9FA5A-Za-z0-9_\\-\\.]{1,30}$"};
std::wregex re(pattern);
std::string test_str = "你啊23zs.-_s";
bool ret = std::regex_match(transformString2Wstring(test_str), re);
if (!ret) {
std::cout << "not match \n";
}
}
|