I have a string of this format that I need to verify:
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.
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).
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).
Simple custom validator if bringing in a regex library isn't worth it ( 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:
#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). Nothing in those should require 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. You might want to try this awesome solution here in SO to work with the regexes in C++<11 without much problem.
Otherwise, go with Cornstalk's answer.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.