Visual Studio 2003使用regex的另一种方法
Alternative way of using regex for Visual Studio 2003
我构建了一个使用regex检查值的示例程序。此示例正在Visual Studio 2012上运行。
但是Regex在VisualStudio2003上并不存在。
我的问题是:在不使用Regex和第三方库的情况下,如何使用Visual Studio 2003检查值?
我的源代码:
#include "stdafx.h"
#include <regex>
#include <string>
using namespace std;
int main()
{
std::string code1 = "{1N851111-8M32-2234-B83K-123456789012}";
std::regex control("^[{]{8}[A-Za-z0-9]{1}[-]{4}[A-Za-z0-9]{1}[-]{4}[A-Za-z0-9]{1}[-]{4}[A-Za-z0-9]{1}[-]{12}[A-Za-z0-9]$[}]");
std::smatch match;
if (std::regex_search(code1, match, control))
{
std::cout << "MAtch found";
}
else
{
std::cout << "Match not found";
}
return 0;
}
好吧,如果你不想使用第三方图书馆(为什么,顺便说一句?),你将不得不一路步行。。。(听起来很简单,不是吗?)
一开始,您的正则表达式似乎并不是您想要的。你试过了吗?至少,这个字符串与您的示例字符串匹配:
std::regex control("^[{][A-Za-z0-9]{8}([-][A-Za-z0-9]{4}){3}[-][A-Za-z0-9]{12}[}]$");
然后让我们看看正则表达式(我将使用我的…):
^
–好吧,从一开始就好,所以我们不必搜索字符串中间的某个地方[{]
–必须是左大括号[A-Za-z0-9]{8}
–后面正好跟八个字母数字字符([-][A-Za-z0-9]{4}){3}
–后面跟着减号的字母数字符号–整个东西三次[-][A-Za-z0-9]{12}
–后面跟着telve字母数字的另一个减号[}]$
–末端的闭合支架
因此:
bool isValid(::std::string const& value)
{
if(value.length() != 38)
return false;
char const* v = value.c_str();
if(*v++ != '{')
return false;
for(char const* end = v + 8; v != end; ++v)
{
if(!isalnum(*v))
return false;
}
for(int i = 0; i < 3; ++i)
{
if(*v++ != '-')
return false;
for(char const* end = v + 4; v != end; ++v)
{
if(!isalnum(*v))
return false;
}
}
if(*v++ != '-')
return false;
for(char const* end = v + 12; v != end; ++v)
{
if(!isalnum(*v))
return false;
}
return *v == '}';
}
相关文章:
- 只需要知道我在c ++中打印模式的方式是否有效,或者有另一种方法可以有效地做到这一点
- 如何在一种方法中创建对象并在另一种方法中使用它
- 常量静态成员函数:有另一种方法可用吗?
- 如何将程序从主发送到另一种方法
- 如何使用shared_ptr或推荐另一种方法(unique_ptr)
- 在C++编程中继续下一行的另一种方法是什么?
- 是否有另一种方法可以在不使用宏定义的情况下进行此操作
- 将带有参数的void方法传递给使用std ::函数和std :: bind c 的另一种方法
- C++:访问由另一种方法返回的向量的常量向量时出现分段错误
- 一个人如何将腋窝VEC/MAT作为REF论点传递给另一种方法
- Visual Studio 2003使用regex的另一种方法
- C++另一种方法计算平均值
- 在cuda中使用静态成员函数模板结构的另一种方法
- C++-使用循环的另一种方法
- 在C++03中初始化成员的另一种方法
- 获取十六进制字符串的字节长度的另一种方法
- 如何让一些派生类使用相同的方法,而另一些派生类则使用另一种方法
- 除以2的另一种方法是什么?
- 是否有另一种方法调用基方法从派生是虚拟的
- 在数据结构中保存成员的另一种方法