[英]Regular Expression Library for C++03
I have a string of this format that I need to verify: 我有一个需要验证的这种格式的字符串:
H12345-001
H12345-001
Here the first character should be an alphabetical character followed by 5 digits, then a dash ( -
), then two zeroes and finally a digit. 在这里,第一个字符应该是字母字符,后跟5个数字,然后是破折号(
-
),然后是两个零,最后是一个数字。
I am not sure if regex is right choice or can do it in a naive way of comparing each character. 我不确定正则表达式是正确的选择还是可以比较每个字符的幼稚方式做到这一点。 If regex is right way to go can someone point out to an example tutorials to use it.
如果正则表达式是正确的选择,那么有人可以指出示例教程来使用它。 I am using C++03 (ie no C++11).
我正在使用C ++ 03(即没有C ++ 11)。
Assuming your string can include a lowercase letter, the regex is [a-zA-z]\\d\\d\\d\\d\\d-00\\d
(just remove the az
if you don't want lowercase letters). 假设您的字符串可以包含小写字母,则正则表达式为
[a-zA-z]\\d\\d\\d\\d\\d-00\\d
(如果您不希望使用小写字母,只需删除az
)。
Simple custom validator if bringing in a regex library isn't worth it ( demo ): 如果引入一个正则表达式库不值得,那么可以使用简单的自定义验证器( demo ):
bool isValid(const std::string& str)
{
return
(str.size() == 10) &&
(('a' <= str[0] && str[0] <= 'z') ||
('A' <= str[0] && str[0] <= 'Z')) &&
('0' <= str[1] && str[1] <= '9') &&
('0' <= str[2] && str[2] <= '9') &&
('0' <= str[3] && str[3] <= '9') &&
('0' <= str[4] && str[4] <= '9') &&
('0' <= str[5] && str[5] <= '9') &&
(str[6] == '-' && str[7] == '0' && str[8] == '0') &&
('0' <= str[9] && str[9] <= '9');
}
You can use Boost.Regex for this: 您可以为此使用Boost.Regex :
#include <iostream>
#include <string>
#include <boost/lexical_cast.hpp>
#include <boost/regex.hpp>
boost::regex your_regex("[A-Z]\\d{5}-00\\d");
boost::smatch match;
int main() {
std::string input = "H12345-001";
std::string output;
if(boost::regex_match(input, match, your_regex)) {
output = boost::lexical_cast<std::string>(match);
std::cout << output << std::endl;
}
return 0;
}
This is an example for matching a single occurrence, you will need to adjust it to your needs. 这是匹配单个事件的示例,您将需要对其进行调整。
Any regex library will do. 任何正则表达式库都可以。 There's pcre for POISX systems, for example, where you just
#include <regex.h>
and call the regcomp
family of functions ( man 3 regcomp for more info). 例如,有用于POISX系统的pcre,您只需
#include <regex.h>
并调用regcomp
系列函数(有关更多信息,请#include <regex.h>
man 3 regcomp )。 Nothing in those should require C++11. 这些都不需要C ++ 11。
Frankly, if you're going with regexes, which for this problem doesn't seem to be worth it, the one difficult issue will be to have the regex pattern proper in the C++ code, because of having to escape all the backslashes and stuff. 坦率地说,如果您要使用正则表达式,对于这个问题似乎不值得,那么一个困难的问题是在C ++代码中使用适当的正则表达式模式,因为必须转义所有反斜杠和其他内容。 You might want to try this awesome solution here in SO to work with the regexes in C++<11 without much problem.
您可能想在SO中尝试这种出色的解决方案,以便在C ++ <11中使用正则表达式时没有太大问题。
Otherwise, go with Cornstalk's answer. 否则,请遵循Cornstalk的答案。
This regex is pretty bad but the easiest to understand that I could come up with that will match what you want. 这个正则表达式非常糟糕,但是最容易理解的是我可以提出的正则表达式将与您想要的匹配。
([A-Z])\d\d\d\d\d-00\d
Here is a tutorial on it like requested: http://en.cppreference.com/w/cpp/regex 这是一个有关要求的教程: http : //en.cppreference.com/w/cpp/regex
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.